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

Introduce new property 'subzero.referenceresolver.class' where we could specify Kryo's ReferenceResolver implementation #25

Merged
merged 2 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ version with regular dependencies:
Default value: 16KB
- System property `subzero.base.type.id` sets base for auto-generated
type id
- System property `subzero.referenceresolver.class` sets the ReferenceResolver implementation. Default value: `com.esotericsoftware.kryo.util.MapReferenceResolver`

## Custom Kryo Serializers
SubZero can use custom Kryo serializers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
package info.jerrinot.subzero.internal.strategy;

import com.esotericsoftware.kryo.ClassResolver;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.ReferenceResolver;
import com.esotericsoftware.kryo.StreamFactory;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.InputChunked;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.io.OutputChunked;
import com.esotericsoftware.kryo.util.DefaultStreamFactory;
import com.esotericsoftware.kryo.util.MapReferenceResolver;
import com.hazelcast.core.HazelcastInstance;
import info.jerrinot.subzero.internal.ClassLoaderUtils;
import info.jerrinot.subzero.internal.IdGeneratorUtils;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static java.lang.Boolean.getBoolean;
import static java.lang.Integer.getInteger;
import static java.lang.String.format;
import static java.lang.System.getProperty;

public abstract class KryoStrategy<T> {

private static final int BUFFER_SIZE = getInteger("subzero.buffer.size.kb", 16) * 1024;
private static final boolean IGNORE_HAZELCAST_CLASSLOADER = getBoolean("subzero.classloading.ignore");

private static final String DEFAULT_REFERENCE_RESOLVER_CLASS = "com.esotericsoftware.kryo.util.MapReferenceResolver";
private static final String REFERENCE_RESOLVER_CLASS_SYSTEM_PROPERTY = "subzero.referenceresolver.class";

private HazelcastInstance hazelcastInstance;

private final ThreadLocal<KryoContext> KRYOS = new ThreadLocal<KryoContext>() {
Expand All @@ -38,13 +44,13 @@ protected KryoContext initialValue() {
private Kryo newKryoInstance() {
Kryo kryo;
if (IGNORE_HAZELCAST_CLASSLOADER) {
kryo = new Kryo();
kryo = new Kryo(createReferenceResolver());
} else {
ClassLoader classLoader = ClassLoaderUtils.getConfiguredClassLoader(hazelcastInstance);
Copy link
Owner

Choose a reason for hiding this comment

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

shouldnt you respect the properties even when the IGNORE_HAZELCAST_CLASSLOADER is set to true ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good question. We could assume that the property com.esotericsoftware.kryo.util.MapReferenceResolver should ignore whether the Hazelcast class loader is ignored or not as we care only about the ReferenceResolver.
So in line 42 we should have sth like this:

kryo = new Kryo(createReferenceResolver());

Do you agree?

Copy link
Owner

Choose a reason for hiding this comment

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

yes. ☝️ looks good.
thanks for the PR!

DelegatingClassResolver classResolver = new DelegatingClassResolver(classLoader);
MapReferenceResolver mapReferenceResolver = new MapReferenceResolver();
DefaultStreamFactory defaultStreamFactory = new DefaultStreamFactory();
kryo = new Kryo(classResolver, mapReferenceResolver, defaultStreamFactory);
ClassResolver classResolver = new DelegatingClassResolver(classLoader);
ReferenceResolver referenceResolver = createReferenceResolver();
StreamFactory defaultStreamFactory = new DefaultStreamFactory();
kryo = new Kryo(classResolver, referenceResolver, defaultStreamFactory);
}
registerCustomSerializers(kryo);
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
Expand All @@ -61,7 +67,7 @@ public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}

public void write(OutputStream out, T object) throws IOException {
public void write(OutputStream out, T object) {
KryoContext kryoContext = KRYOS.get();
OutputChunked output = kryoContext.getOutputChunked();
output.setOutputStream(out);
Expand All @@ -72,12 +78,11 @@ public void write(OutputStream out, T object) throws IOException {

abstract void writeObject(Kryo kryo, Output output, T object);

public T read(InputStream in) throws IOException {
public T read(InputStream in) {
KryoContext kryoContext = KRYOS.get();
InputChunked input = kryoContext.getInputChunked();
input.setInputStream(in);
T object = readObject(kryoContext.getKryo(), input);
return object;
return readObject(kryoContext.getKryo(), input);
}

abstract T readObject(Kryo kryo, Input input);
Expand All @@ -87,4 +92,15 @@ public void destroy(HazelcastInstance hazelcastInstance) {
}

public abstract int newId();

private static ReferenceResolver createReferenceResolver() {
String referenceResolverClass = getProperty(REFERENCE_RESOLVER_CLASS_SYSTEM_PROPERTY, DEFAULT_REFERENCE_RESOLVER_CLASS);
try {
Class<?> resolverClass = Class.forName(referenceResolverClass);
return (ReferenceResolver) resolverClass.getConstructor().newInstance();

} catch (Exception e) {
throw new IllegalArgumentException(format("could not create ReferenceResolver with class %s", referenceResolverClass), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package info.jerrinot.subzero.internal.strategy;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.ReferenceResolver;
import info.jerrinot.subzero.UserSerializer;
import org.junit.Test;

import java.lang.reflect.Field;

import static org.junit.Assert.assertEquals;

public class KryoStrategyCustomReferenceResolverTest {

@SuppressWarnings("unchecked")
@Test
public void should_use_strategy_with_specified_reference_resolver() throws Exception {
// given
System.setProperty("subzero.referenceresolver.class", "info.jerrinot.subzero.internal.strategy.NullReferenceResolver");
GlobalKryoStrategy kryoStrategy = new GlobalKryoStrategy(NULL_USER_SERIALIZER);

// when
Field field = KryoStrategy.class.getDeclaredField("KRYOS");
field.setAccessible(true);
Class<? extends ReferenceResolver> actualClassResolver = ((ThreadLocal<KryoContext>) field.get(kryoStrategy))
.get().getKryo().getReferenceResolver().getClass();

// then
assertEquals(NullReferenceResolver.class, actualClassResolver);
}

private static final UserSerializer NULL_USER_SERIALIZER = new UserSerializer() {
@Override
public void registerSingleSerializer(Kryo kryo, Class clazz) {
// do nothing
}
@Override
public void registerAllSerializers(Kryo kryo) {
// do nothing
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package info.jerrinot.subzero.internal.strategy;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.ReferenceResolver;

public class NullReferenceResolver implements ReferenceResolver {
@Override
public void setKryo(Kryo kryo) {
// do nothing
}

@Override
public int getWrittenId(Object object) {
return 0;
}

@Override
public int addWrittenObject(Object object) {
return 0;
}

@Override
public int nextReadId(Class type) {
return 0;
}

@Override
public void setReadObject(int id, Object object) {
// do nothing
}

@Override
public Object getReadObject(Class type, int id) {
return null;
}

@Override
public void reset() {
// do nothing
}

@Override
public boolean useReferences(Class type) {
return false;
}
}