Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@
import org.apache.hudi.exception.HoodieSerializationException;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.FieldSerializer;
import com.esotericsoftware.reflectasm.ConstructorAccess;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
* {@link SerializationUtils} class internally uses {@link Kryo} serializer for serializing / deserializing objects.
Expand Down Expand Up @@ -121,50 +116,16 @@ private static class KryoInstantiator implements Serializable {

public Kryo newKryo() {

Kryo kryo = new KryoBase();
Kryo kryo = new Kryo();
// ensure that kryo doesn't fail if classes are not registered with kryo.
kryo.setRegistrationRequired(false);
// This would be used for object initialization if nothing else works out.
kryo.setInstantiatorStrategy(new org.objenesis.strategy.StdInstantiatorStrategy());
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
Comment thread
vinothchandar marked this conversation as resolved.
// Handle cases where we may have an odd classloader setup like with libjars
// for hadoop
kryo.setClassLoader(Thread.currentThread().getContextClassLoader());
return kryo;
}

private static class KryoBase extends Kryo {
@Override
protected Serializer newDefaultSerializer(Class type) {
Comment thread
lamberken marked this conversation as resolved.
final Serializer serializer = super.newDefaultSerializer(type);
if (serializer instanceof FieldSerializer) {
final FieldSerializer fieldSerializer = (FieldSerializer) serializer;
fieldSerializer.setIgnoreSyntheticFields(true);
}
return serializer;
}

@Override
protected ObjectInstantiator newInstantiator(Class type) {
return () -> {
// First try reflectasm - it is fastest way to instantiate an object.
try {
final ConstructorAccess access = ConstructorAccess.get(type);
return access.newInstance();
} catch (Throwable t) {
// ignore this exception. We may want to try other way.
}
// fall back to java based instantiation.
try {
final Constructor constructor = type.getConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException
| InvocationTargetException e) {
// ignore this exception. we will fall back to default instantiation strategy.
}
return super.getInstantiatorStrategy().newInstantiatorOf(type).newInstance();
Comment thread
lamberken marked this conversation as resolved.
};
}
}
}
}