Skip to content

Commit

Permalink
Update kryo to 4.0.0 and kryo serializers to 0.41 (#331)
Browse files Browse the repository at this point in the history
* Update kryo library from version 3.0.3 to 4.0.0 and kryo serializers library from version 0.37 to 0.41.
  Modified the builder to support turning field generics off/on and exposed the Kryo Pool from the transcoder if you it for something other than session map serialization.

* Since I override MemcachedBackupSessionManager I need access to the hosting valve for extra processing.
  • Loading branch information
JamieGHamilton authored and magro committed Jan 3, 2017
1 parent 6cb1c45 commit f71b995
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ void setStorageClient(final StorageClient storage) {
_storage = storage;
}

RequestTrackingHostValve getTrackingHostValve() {
public RequestTrackingHostValve getTrackingHostValve() {
return _trackingHostValve;
}

Expand Down
4 changes: 2 additions & 2 deletions kryo-serializer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.37</version>
<version>0.41</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>3.0.3</version>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public Kryo build() {
}

protected Kryo createKryo(ClassResolver classResolver, ReferenceResolver referenceResolver, StreamFactory streamFactory) {
return new Kryo(classResolver, referenceResolver, streamFactory);
Kryo kryo = new Kryo(classResolver, referenceResolver, streamFactory);
// Maintain Kryo compatibility (pre version 4) - can turn this off by calling withOptimizedGenerics(false)
kryo.getFieldSerializerConfig().setOptimizedGenerics(true);
return kryo;
}

public KryoBuilder withClassResolver(final ClassResolver classResolver) {
Expand Down Expand Up @@ -117,4 +120,14 @@ public Kryo build() {
};
}

public KryoBuilder withOptimizedGenerics(final boolean optimizedGenerics) {
return new KryoBuilder() {
@Override
public Kryo build() {
Kryo k = this.buildFrom(KryoBuilder.this);
k.getFieldSerializerConfig().setOptimizedGenerics(optimizedGenerics);
return k;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import de.javakaffee.web.msm.MemcachedBackupSession;
import de.javakaffee.web.msm.SessionAttributesTranscoder;
import de.javakaffee.web.msm.TranscoderDeserializationException;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -149,26 +152,26 @@ protected Kryo createKryo(final ClassResolver classResolver, final ReferenceReso
private final List<SerializerFactory> serializerFactories = load(SerializerFactory.class, customConverterClassNames, classLoader, this);

@Override
@SuppressWarnings( { "rawtypes", "unchecked" } )
public Serializer getDefaultSerializer(final Class clazz) {
final Serializer customSerializer = loadCustomSerializer( clazz, serializerFactories );
@SuppressWarnings( { "rawtypes" } )
public Serializer<?> getDefaultSerializer(final Class clazz) {
final Serializer<?> customSerializer = loadCustomSerializer( clazz, serializerFactories );
if ( customSerializer != null ) {
return customSerializer;
}
if ( copyCollectionsForSerialization ) {
// could also be installed via addDefaultSerializer
final Serializer copyCollectionSerializer = loadCopyCollectionSerializer( clazz );
final Serializer<?> copyCollectionSerializer = loadCopyCollectionSerializer( clazz );
if ( copyCollectionSerializer != null ) {
return copyCollectionSerializer;
}
}
return super.getDefaultSerializer( clazz );
}

private Serializer loadCustomSerializer(final Class<?> clazz, List<SerializerFactory> serializerFactories) {
private Serializer<?> loadCustomSerializer(final Class<?> clazz, List<SerializerFactory> serializerFactories) {
if ( serializerFactories != null ) {
for (SerializerFactory serializerFactory : serializerFactories) {
final Serializer serializer = serializerFactory.newSerializer(clazz);
final Serializer<?> serializer = serializerFactory.newSerializer(clazz);
if (serializer != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Loading custom serializer " + serializer.getClass().getName() + " for class " + clazz);
Expand All @@ -183,7 +186,7 @@ private Serializer loadCustomSerializer(final Class<?> clazz, List<SerializerFac
};
}

private Serializer loadCopyCollectionSerializer( final Class<?> clazz ) {
private Serializer<?> loadCopyCollectionSerializer( final Class<?> clazz ) {
if ( Collection.class.isAssignableFrom( clazz ) ) {
if ( LOG.isDebugEnabled() ) {
LOG.debug( "Loading CopyForIterateCollectionSerializer for class " + clazz );
Expand All @@ -206,11 +209,15 @@ private Serializer loadCopyCollectionSerializer( final Class<?> clazz ) {
@Override
public ConcurrentMap<String, Object> deserializeAttributes(final byte[] data ) {
final Kryo kryo = _kryoPool.borrow();
Input in = null;
try {
return kryo.readObject(new Input(data), ConcurrentHashMap.class);
in = kryo.getStreamFactory().getInput(data);
return kryo.readObject(in, ConcurrentHashMap.class);
} catch ( final RuntimeException e ) {
throw new TranscoderDeserializationException( e );
} finally {
closeSilently(in);
kryo.reset(); // to be safe
_kryoPool.release(kryo);
}
}
Expand All @@ -221,20 +228,37 @@ public ConcurrentMap<String, Object> deserializeAttributes(final byte[] data ) {
@Override
public byte[] serializeAttributes( final MemcachedBackupSession session, final ConcurrentMap<String, Object> attributes ) {
final Kryo kryo = _kryoPool.borrow();
Output out = null;
try {
/**
* Creates an ObjectStream with an initial buffer size of 50KB and a maximum size of 1000KB.
*/
Output out = new Output(_initialBufferSize, _maxBufferSize);
out = kryo.getStreamFactory().getOutput(_initialBufferSize, _maxBufferSize);
kryo.writeObject(out, attributes);
return out.toBytes();
} catch ( final RuntimeException e ) {
throw new TranscoderDeserializationException( e );
} finally {
closeSilently(out);
kryo.reset(); // to be safe
_kryoPool.release(kryo);
}
}

public KryoPool getKryoPool() {
return _kryoPool;
}

private void closeSilently( Closeable closeable ) {
if ( closeable != null ) {
try {
closeable.close();
} catch ( final IOException f ) {
// fail silently
}
}
}

private <T> List<T> load( Class<T> type, final String[] customConverterClassNames, final ClassLoader classLoader) {
return load(type, customConverterClassNames, classLoader, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Kryo build(DefaultClassResolver classResolver,
.withInstantiatorStrategy(instantiatorStrategy)
.withRegistrationRequired(true) // Kryo default is false
.withReferences(false) // Kryo default is true
.withOptimizedGenerics(false)
.build();
}
} },
Expand All @@ -71,6 +72,7 @@ Kryo build(DefaultClassResolver classResolver,
.withInstantiatorStrategy(instantiatorStrategy)
.withRegistrationRequired(true) // Kryo default is false
.withReferences(false) // Kryo default is true
.withOptimizedGenerics(false)
.withKryoCustomization(enableAsm)
.withKryoCustomization(registerMyCollectionSerializer)
.build();
Expand All @@ -89,7 +91,7 @@ public void testKryoBuilder(BuildKryo buildKryo) {

KryoCustomization enableAsm = new KryoCustomization() {
@Override public void customize(Kryo kryo) {
kryo.setAsmEnabled(true); // Kryo default false
kryo.getFieldSerializerConfig().setUseAsm(true); // Kryo default false
}
};

Expand All @@ -108,8 +110,9 @@ public void testKryoBuilder(BuildKryo buildKryo) {
assertSame(kryo.getInstantiatorStrategy(), instantiatorStrategy);
assertTrue(kryo.isRegistrationRequired());
assertFalse(kryo.getReferences());
assertTrue(kryo.getAsmEnabled());
assertTrue(kryo.getFieldSerializerConfig().isUseAsm());
assertSame(kryo.getDefaultSerializer(Collection.class), collectionSerializer);
assertFalse(kryo.getFieldSerializerConfig().isOptimizedGenerics());
}

static abstract class BuildKryo {
Expand Down

0 comments on commit f71b995

Please sign in to comment.