Fix data race in JsonPayloadSerializerProvider.#38265
Fix data race in JsonPayloadSerializerProvider.#38265stankiewicz wants to merge 2 commits intoapache:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical data race that arose from the serialization of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors JsonPayloadSerializerProvider to use a private inner class, JsonPayloadSerializer, for handling serialization and deserialization with lazy-loaded ObjectMapper instances. The review feedback suggests several improvements for concurrency and code cleanliness: marking the mapper fields as volatile to ensure visibility, implementing double-checked locking to reduce synchronization overhead in high-throughput scenarios, and removing redundant null initializations in the constructor.
| private transient @Nullable ObjectMapper deserializeMapper; | ||
| private transient @Nullable ObjectMapper serializeMapper; |
There was a problem hiding this comment.
The ObjectMapper fields should be marked as volatile to ensure proper visibility when using lazy initialization, especially if using double-checked locking to avoid synchronization overhead.
| private transient @Nullable ObjectMapper deserializeMapper; | |
| private transient @Nullable ObjectMapper serializeMapper; | |
| private transient volatile @Nullable ObjectMapper deserializeMapper; | |
| private transient volatile @Nullable ObjectMapper serializeMapper; |
| public JsonPayloadSerializer(Schema schema) { | ||
| this.schema = schema; | ||
| this.deserializeMapper = null; | ||
| this.serializeMapper = null; | ||
| } |
| private synchronized ObjectMapper getDeserializeMapper() { | ||
| if (deserializeMapper == null) { | ||
| deserializeMapper = RowJsonUtils.newObjectMapperWith(RowJsonDeserializer.forSchema(schema)); | ||
| } | ||
| return deserializeMapper; | ||
| } | ||
|
|
||
| private synchronized ObjectMapper getSerializeMapper() { | ||
| if (serializeMapper == null) { | ||
| serializeMapper = RowJsonUtils.newObjectMapperWith(RowJsonSerializer.forSchema(schema)); | ||
| } | ||
| return serializeMapper; | ||
| } |
There was a problem hiding this comment.
Using synchronized on the entire method for every access to the ObjectMapper can introduce contention in high-throughput scenarios, as these methods are called for every record processed. Consider using double-checked locking (with the fields marked as volatile) to optimize the common path where the mappers are already initialized.
private ObjectMapper getDeserializeMapper() {
ObjectMapper result = deserializeMapper;
if (result == null) {
synchronized (this) {
result = deserializeMapper;
if (result == null) {
deserializeMapper = result = RowJsonUtils.newObjectMapperWith(RowJsonDeserializer.forSchema(schema));
}
}
}
return result;
}
private ObjectMapper getSerializeMapper() {
ObjectMapper result = serializeMapper;
if (result == null) {
synchronized (this) {
result = serializeMapper;
if (result == null) {
serializeMapper = result = RowJsonUtils.newObjectMapperWith(RowJsonSerializer.forSchema(schema));
}
}
}
return result;
}…ile and implement double-checked locking
|
Assigning reviewers: R: @chamikaramj for label java. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
The anonymous class returned by
getSerializercapturedObjectMapperinstances. When these were serialized (e.g., during pipeline translation inDirectRunner), a data race occurred in Jackson'sDeserializerCache.writeReplace()due to concurrent modification of internal caches during serialization.This change introduces a concrete
JsonPayloadSerializerclass that marks theObjectMapperinstances astransientand initializes them lazily. This avoids serializing the mappers and eliminates the data race.Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.