-
Notifications
You must be signed in to change notification settings - Fork 2.9k
NIFI-5805: Pool the BinaryEncoders used by the WriteAvroResultWithExt… #3160
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
+152
−47
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,40 +17,51 @@ | |
|
|
||
| package org.apache.nifi.avro; | ||
|
|
||
| import java.io.BufferedOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.generic.GenericDatumWriter; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.avro.io.BinaryEncoder; | ||
| import org.apache.avro.io.DatumWriter; | ||
| import org.apache.avro.io.EncoderFactory; | ||
| import org.apache.nifi.logging.ComponentLog; | ||
| import org.apache.nifi.schema.access.SchemaAccessWriter; | ||
| import org.apache.nifi.serialization.AbstractRecordSetWriter; | ||
| import org.apache.nifi.serialization.record.Record; | ||
| import org.apache.nifi.serialization.record.RecordSchema; | ||
|
|
||
| import java.io.BufferedOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.util.Map; | ||
| import java.util.concurrent.BlockingQueue; | ||
|
|
||
| public class WriteAvroResultWithExternalSchema extends AbstractRecordSetWriter { | ||
| private final SchemaAccessWriter schemaAccessWriter; | ||
| private final RecordSchema recordSchema; | ||
| private final Schema avroSchema; | ||
| private final BinaryEncoder encoder; | ||
| private final OutputStream buffered; | ||
| private final DatumWriter<GenericRecord> datumWriter; | ||
| private final BlockingQueue<BinaryEncoder> recycleQueue; | ||
|
|
||
| public WriteAvroResultWithExternalSchema(final Schema avroSchema, final RecordSchema recordSchema, | ||
| final SchemaAccessWriter schemaAccessWriter, final OutputStream out) throws IOException { | ||
| public WriteAvroResultWithExternalSchema(final Schema avroSchema, final RecordSchema recordSchema, final SchemaAccessWriter schemaAccessWriter, | ||
| final OutputStream out, final BlockingQueue<BinaryEncoder> recycleQueue, final ComponentLog logger) { | ||
| super(out); | ||
| this.recordSchema = recordSchema; | ||
| this.schemaAccessWriter = schemaAccessWriter; | ||
| this.avroSchema = avroSchema; | ||
| this.buffered = new BufferedOutputStream(out); | ||
| this.recycleQueue = recycleQueue; | ||
|
|
||
| BinaryEncoder reusableEncoder = recycleQueue.poll(); | ||
| if (reusableEncoder == null) { | ||
| logger.debug("Was not able to obtain a BinaryEncoder from reuse pool. This is normal for the first X number of iterations (where X is equal to the max size of the pool), " + | ||
| "but if this continues, it indicates that increasing the size of the pool will likely yield better performance for this Avro Writer."); | ||
| } | ||
|
|
||
| encoder = EncoderFactory.get().blockingBinaryEncoder(buffered, reusableEncoder); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably, we should add a debug log here to provide information whether current number of pool size fits the actual usage. If there are more null reusableEncorder and user want to improve performance, then they can increase pool size ... etc. |
||
|
|
||
| datumWriter = new GenericDatumWriter<>(avroSchema); | ||
| encoder = EncoderFactory.get().blockingBinaryEncoder(buffered, null); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -88,4 +99,13 @@ public void flush() throws IOException { | |
| public String getMimeType() { | ||
| return "application/avro-binary"; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| if (encoder != null) { | ||
| recycleQueue.offer(encoder); | ||
| } | ||
|
|
||
| super.close(); | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
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.
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.
Since this configuration is performance related and depends on environment, I'd suggest supporting variable registry EL.