-
Notifications
You must be signed in to change notification settings - Fork 198
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
Improve OpenSearch sink performance within the opensearch-java client #1391
Merged
dlvenable
merged 1 commit into
opensearch-project:main
from
dlvenable:opensearch-java-client-performance
May 13, 2022
Merged
Changes from all commits
Commits
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 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 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 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 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
84 changes: 84 additions & 0 deletions
84
...in/java/com/amazon/dataprepper/plugins/sink/opensearch/bulk/PreSerializedJsonpMapper.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.sink.opensearch.bulk; | ||
|
||
import jakarta.json.spi.JsonProvider; | ||
import jakarta.json.stream.JsonGenerator; | ||
import jakarta.json.stream.JsonParser; | ||
import org.opensearch.client.json.JsonpMapper; | ||
import org.opensearch.client.json.jackson.JacksonJsonProvider; | ||
import org.opensearch.client.json.jackson.JacksonJsonpGenerator; | ||
import org.opensearch.client.json.jackson.JacksonJsonpMapper; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* An implementation of the opensearch-java client's {@link JsonpMapper}. It can avoid duplicate | ||
* serialization by use of the {@link SerializedJson} interface. For values that inherit from | ||
* {@link SerializedJson}, it will not re-serialize these. For other values, it uses Jackson | ||
* serialization via the {@link JacksonJsonpMapper}. | ||
*/ | ||
public class PreSerializedJsonpMapper implements JsonpMapper { | ||
|
||
private final JsonpMapper innerMapper; | ||
private final PreSerializedJsonProvider jsonProvider; | ||
|
||
public PreSerializedJsonpMapper() { | ||
innerMapper = new JacksonJsonpMapper(); | ||
jsonProvider = new PreSerializedJsonProvider(); | ||
} | ||
|
||
@Override | ||
public JsonProvider jsonProvider() { | ||
return jsonProvider; | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(JsonParser parser, Class<T> clazz) { | ||
return innerMapper.deserialize(parser, clazz); | ||
} | ||
|
||
@Override | ||
public <T> void serialize(T value, JsonGenerator generator) { | ||
if(value instanceof SerializedJson) { | ||
if (! (generator instanceof PreSerializedJsonGenerator)) | ||
throw new IllegalArgumentException("Unsupported JsonGenerator"); | ||
|
||
final OutputStream outputStream = ((PreSerializedJsonGenerator) generator).outputStream; | ||
|
||
try { | ||
outputStream.write(((SerializedJson) value).getSerializedJson()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} else { | ||
innerMapper.serialize(value, generator); | ||
} | ||
} | ||
|
||
private static class PreSerializedJsonGenerator extends JacksonJsonpGenerator { | ||
private OutputStream outputStream; | ||
|
||
public PreSerializedJsonGenerator(com.fasterxml.jackson.core.JsonGenerator generator, OutputStream outputStream) { | ||
super(generator); | ||
this.outputStream = outputStream; | ||
} | ||
} | ||
|
||
|
||
private static class PreSerializedJsonProvider extends JacksonJsonProvider { | ||
@Override | ||
public JsonGenerator createGenerator(OutputStream out) { | ||
try { | ||
return new PreSerializedJsonGenerator(jacksonJsonFactory().createGenerator(out), out); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rch/src/main/java/com/amazon/dataprepper/plugins/sink/opensearch/bulk/SerializedJson.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.sink.opensearch.bulk; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Represents JSON which is already serialized for use in the {@link PreSerializedJsonpMapper}. | ||
*/ | ||
public interface SerializedJson extends SizedDocument { | ||
|
||
byte[] getSerializedJson(); | ||
|
||
/** | ||
* Creates a new {@link SerializedJson} from a JSON string. | ||
* | ||
* @param jsonString The serialized JSON string which forms this JSON data. | ||
* @return A new {@link SerializedJson}. | ||
*/ | ||
static SerializedJson fromString(String jsonString) { | ||
Objects.requireNonNull(jsonString); | ||
return new SerializedJsonImpl(jsonString.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...src/main/java/com/amazon/dataprepper/plugins/sink/opensearch/bulk/SerializedJsonImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.sink.opensearch.bulk; | ||
|
||
class SerializedJsonImpl implements SerializedJson { | ||
private byte[] document; | ||
|
||
public SerializedJsonImpl(final byte[] document) { | ||
this.document = document; | ||
} | ||
|
||
@Override | ||
public long getDocumentSize() { | ||
return document.length; | ||
} | ||
|
||
@Override | ||
public byte[] getSerializedJson() { | ||
return document; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...arch/src/main/java/com/amazon/dataprepper/plugins/sink/opensearch/bulk/SizedDocument.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.sink.opensearch.bulk; | ||
|
||
/** | ||
* Represents an OpenSearch document with a serialized document size. | ||
*/ | ||
public interface SizedDocument { | ||
/** | ||
* The size of this document. | ||
* | ||
* @return The document size in bytes | ||
*/ | ||
long getDocumentSize(); | ||
} |
41 changes: 0 additions & 41 deletions
41
...arch/src/main/java/com/amazon/dataprepper/plugins/sink/opensearch/bulk/SizedJsonData.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Why do we need the normalization?
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.
This is because of the assertions in the tests that assert the number of bytes written. The JSON is pretty formatted. So for
Record<String>
these are larger strings (and thus more bytes) thanRecord<Event>
. The make the assertions later simpler, I just make all JSON non-pretty and thus the same byte size.