Skip to content
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

Add Kryo.setClassResolver(ClassResolver) #182

Closed
wants to merge 1 commit into from

Conversation

magro
Copy link
Collaborator

@magro magro commented Jan 20, 2014

Add the possibility to set a ClassResolver after Kryo was constructed, but it's expected
that Kryo.setClassResolver is invoked before any (de)serialization is performed (other behaviour
is just not tested).

The Kryo instance is set on the new class resolver, so that the client does not have
to invoke this (also setters for ReferenceResolver and StreamFactory are adjusted to set Kryo).

@magro
Copy link
Collaborator Author

magro commented Jan 20, 2014

  1. Since it is not so obvious, I think it should be mentioned in the JavaDoc that all registrations from the current classResolver of the Kryo instance will be re-registered in the new resolver by default.

You're right, I'll add this to javadoc.

  1. BTW, should this copying behavior be made configurable (if so, may be setClassLoader should have a flag for indicating it)? Are we sure, we always want to copy all registrations?

Sounds good, I can add a flag to setClassResolver that triggers copying of all registrations (if false, only registrations for primitives are copied).

All existing registrations are copied from the previous class resolver to the new one
if copyAllRegistrations is `true`, otherwise only registrations for primitive types
are copied.

The kryo instance is set on the new class resolver, so that the client does
not have to invoke this.

Also setters for ReferenceResolver and StreamFactory are adjusted to set
kryo on the instances, the Kryo constructor now just uses these setters.
@magro
Copy link
Collaborator Author

magro commented Jan 20, 2014

@romix I updated the commit with the proposed changes.

@romix romix mentioned this pull request Jan 24, 2014
@@ -969,6 +961,41 @@ public ClassResolver getClassResolver () {
return classResolver;
}

/**
* Sets the new class resolver and may copy all registrations from the former class resolver. Is expected to be invoked before
* any serialization/deserialization is performed (other behaviour is not tested).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should throw illegal state exception if the method is called at the wrong time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I detect this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but it's strange to allow if the behavior is undefined. If we don't copy I guess it's moot.

@NathanSweet
Copy link
Member

I'm not sure about the general usefulness of allowing the class resolver to change. Why do you want this? Is it app specific? Would it make more sense to do it differently? Eg create your Kryo instance with a class resolver that delegates to another class resolver.

@NathanSweet
Copy link
Member

Currently specifying a ClassResolver in the constructor is mostly about which one to use, not how it is configured. If we'll have setClassResolver I think it should just be a setter for the most part and not try to copy registrations. Copying can happen outside of Kryo. I'm not sure it would be useful, but a ClassResolver may not need to keep a list of registrations, or whatever list it has may not be useful for copying. Eg, maybe the ClassResolver is doing network lookups. So let's not have ClassResolver#getRegistrations(), though implementations may have it to facilitate copying outside of setClassResovler.

@magro
Copy link
Collaborator Author

magro commented Jan 24, 2014

Why do you want this? Is it app specific? Would it make more sense to do it differently? Eg create your Kryo instance with a class resolver that delegates to another class resolver.

In memcached-session-manager (still using kryo1, I want to move to kryo2) it's possible to customize Kryo via a KryoCustomization, implementations can be specified in tomcats context.xml via an attribute customConverter (see SetupAndConfiguration).

A case where I need to customize the ClassResolver is the CGLibProxySerializer, see this CGLibProxySerializerTest.
So in msm the user can register a KryoCustomization for this CGLibProxySerializer to register the cglib-supporting DefaultClassResolver (msm actually already provides a KryoCustomization for the CGLibProxySerializer for kryo1).
I cannot specify the cglib-supporting DefaultClassResolver at compile time because msm should not depend on cglib.

@NathanSweet
Copy link
Member

Maybe it's cleaner to pass around an instance of your own class that collects configuration information, then use that to construct the Kryo instance?

Otherwise, I think a setClassResolver would be ok if its just a (relatively) simple setter.

@magro
Copy link
Collaborator Author

magro commented Jan 24, 2014

Maybe it's cleaner to pass around an instance of your own class that collects configuration information, then use that to construct the Kryo instance?

Yes, probably this way is cleaner, I start to like that idea, will think a bit more about this. Thanks!

@romix
Copy link
Collaborator

romix commented May 26, 2014

@magro Speaking of passing around an instance which collects configuration information and then uses it to construct Kryo instances: How about something like https://github.com/twitter/chill/blob/develop/chill-java/src/main/java/com/twitter/chill/KryoInstantiator.java provided by Chill? Also have a look at other classes in the same directory, e.g. IKryoRegistrar and its implementations. Is it what you have in mind?

@magro
Copy link
Collaborator Author

magro commented May 26, 2014

@romix Probably I'd use a builder to configure kryo.
Am 26.05.2014 17:50 schrieb "romix" [email protected]:

@magro https://github.com/magro Speaking of passing around an instance
which collects configuration information and then uses it to construct Kryo
instances: How about something like
https://github.com/twitter/chill/blob/develop/chill-java/src/main/java/com/twitter/chill/KryoInstantiator.javaprovided by Chill? Also have a look at other classes in the same directory,
e.g. IKryoRegistrar and its implementations. Is it what you have in mind?


Reply to this email directly or view it on GitHubhttps://github.com//pull/182#issuecomment-44199980
.

@magro
Copy link
Collaborator Author

magro commented Nov 1, 2015

I've implemented a builder based approach in msm, therefore obsolete.

@magro magro closed this Nov 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants