-
Notifications
You must be signed in to change notification settings - Fork 226
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
Tweak class caching in node (de)serializers. #1518
Conversation
Opening this as a draft because it still needs a test added, but I still have to think about how exactly to test with loading classes in. |
smithy-model/src/main/java/software/amazon/smithy/model/node/IdentityClassCache.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/node/IdentityClassCache.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/node/IdentityClassCache.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/node/IdentityClassCache.java
Outdated
Show resolved
Hide resolved
46f5941
to
af4f91c
Compare
Looking at this again and trying to update some of the comments, something started to seem not quite right. @mtdowling I was going off your suggestions, but either this implementation isn't exactly what we want, or it needs to be integrated into the node (de)serializers differently. See the diffs DefaultNodeDeserializers:626 and DefaultNodeSerializers:259: the class that is being cached, ObjectMapper and ClassInfo respectively, do not have the same class as the keys of the cache, Class and Class respectively. I added failing test case to show why this doesn't work. Maybe we can use the previous implementation and this new one together, or we need to be able to use a different class as the key and value in IdentityClassCache. |
smithy-model/src/main/java/software/amazon/smithy/model/node/IdentityClassCache.java
Outdated
Show resolved
Hide resolved
Prior to this PR, class caching mechanisms in DefaultNodeDeserializers and DefaultNodeSerializers would cache classes the first time they were loaded. If the class was loaded again using a new ClassLoader, and then retrieved from the cache, there would be a mismatch in the loaded class and the one retrieved from the cache because class equality is dependent on the ClassLoader used. This caused class cast exceptions in the language server. Based on discussion with Michael offline, this PR introduces a modified caching mechanism that associates a key-value pair with an instance of a loaded class. When retrieving a value from the cache, the associated class is also provided, and the cache will be invalidated if the associated class is not referentially equal to the currently cached class. This modified caching mechanism replaces caches in DefaultNodeSerializers and DefaultNodeDeserializers. Testing ------ - Test class for the new caching mechanism - Added test case to ModelAssemblerTest for loading external jars with trait implementations multiple times. The jar and model used for this test case was taken from the language server.
af4f91c
to
8b17ce1
Compare
smithy-model/src/main/java/software/amazon/smithy/model/node/IdentityClassCache.java
Show resolved
Hide resolved
smithy-model/src/test/java/software/amazon/smithy/model/node/IdentityClassCacheTest.java
Show resolved
Hide resolved
Hey @milesziemer, can you share an estimate of when this might be released? It'd be much appreciated, thanks! |
Prior to this PR, class caching mechanisms in DefaultNodeDeserializers
and DefaultNodeSerializers would cache classes the first time they were
loaded. If the class was loaded again using a new ClassLoader, and then
retrieved from the cache, there would be a mismatch in the loaded class
and the one retrieved from the cache because class equality is dependent
on the ClassLoader used. This caused class cast exceptions in the language
server.
Based on discussion with Michael offline, this PR introduces a modified
caching mechanism that associates a key-value pair with an instance
of a loaded class. When retrieving a value from the cache, the associated
class is also provided, and the cache will be invalidated if the associated
class is not referentially equal to the currently cached class. This modified
caching mechanism replaces caches in DefaultNodeSerializers and
DefaultNodeDeserializers.
Testing
implementations multiple times. The jar and model used for this test case
was taken from the language server.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.