Fix DoFnInvoker cache collision for generic types #37354
Closed
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.
Fixes #37351
Description
This PR addresses a critical bug in the
ByteBuddyDoFnInvokerFactorywhere the caching mechanism for generatedDoFnInvokerclasses failed to distinguish between different generic instantiations of the sameDoFnclass.1. The Problem: Cache Collision on Generic Types
The previous implementation utilized a
ConcurrentHashMap<Class<?>, Constructor<?>>to cache the generated invoker constructors. The cache key was solely theDoFnclass itself.In scenarios involving generic
DoFnclasses (e.g.,class MyGenericFn<T> extends DoFn<T, T>), the raw class object is identical regardless of the type parameterT.MyGenericFn<String>and laterMyGenericFn<Integer>, the factory would generate an invoker forStringand cache it.MyGenericFn<Integer>is requested, the factory hits the cache using the sameMyGenericFn.classkey and returns theString-specialized invoker.ClassCastExceptionor incorrectCoderinference, as the bytecode for the invoker is specialized for the wrong type.2. The Solution: Type-Aware Caching & Naming
This PR introduces a composite cache key and updates the class naming strategy to ensure full isolation between generic types.
A. Introduced
InvokerCacheKeyI replaced the simple
Class<?>key with a new static inner classInvokerCacheKey. This key encapsulates three components:fnClass: The raw class of the DoFn.inputType: TheTypeDescriptorfor the input element.outputType: TheTypeDescriptorfor the output element.By implementing
equals()andhashCode()based on all three fields, we ensure thatMyGenericFn<String>andMyGenericFn<Integer>map to distinct cache entries.B. Unique Class Naming Strategy
ByteBuddy cannot define two different classes with the exact same name in the same
ClassLoader. To support multiple generated invokers for the same rawDoFnclass, I modifiedgenerateInvokerClass.<DoFnClassName>$DoFnInvoker<DoFnClassName>$DoFnInvoker$<TypeHash>The suffix now includes a hexadecimal hash derived from the input and output
TypeDescriptors. This guarantees that the generated bytecode class names are unique for each generic instantiation.Test Plan
I have added a regression test
testCacheKeyCollisionProoftoDoFnInvokersTest.java.DynamicTypeDoFn<T>and manually forces specificTypeDescriptorreturns.String, once forInteger) and asserts that the generated Invoker classes are not the same (assertNotSame).Existing tests passed:
DoFnInvokersTestThank 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.