Skip to content

Commit

Permalink
feature: support kryo 5.3.0 (apache#4968)
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaLionLi authored Oct 1, 2022
1 parent d0f324c commit dac8aa8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 65 deletions.
2 changes: 2 additions & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#4863](https://github.com/seata/seata/pull/4863)] support oracle and postgresql multi primary key
- [[#4649](https://github.com/seata/seata/pull/4649)] seata-server support multiple registry
- [[#4479](https://github.com/seata/seata/pull/4479)] TCC mode supports tcc annotation marked on both interface and implementation class
- [[#4468](https://github.com/seata/seata/pull/4968)] support kryo 5.3.0


### bugfix:
Expand Down Expand Up @@ -65,5 +66,6 @@ Thanks to these contributors for their code commits. Please report an unintended
- [doubleDimple](https://github.com/doubleDimple)
- [jsbxyyx](https://github.com/jsbxyyx)
- [tuwenlin](https://github.com/tuwenlin)
- [CrazyLionLi](https://github.com/JavaLionLi)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
2 changes: 2 additions & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [[#4863](https://github.com/seata/seata/pull/4863)] support oracle and postgresql multi primary key
- [[#4649](https://github.com/seata/seata/pull/4649)] seata-server支持多注册中心
- [[#4479](https://github.com/seata/seata/pull/4479)] TCC注解支持添加在实现类及其方法上也生效
- [[#4468](https://github.com/seata/seata/pull/4968)] 支持kryo 5.3.0


### bugfix:
Expand Down Expand Up @@ -65,5 +66,6 @@
- [doubleDimple](https://github.com/doubleDimple)
- [jsbxyyx](https://github.com/jsbxyyx)
- [tuwenlin](https://github.com/tuwenlin)
- [CrazyLionLi](https://github.com/JavaLionLi)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
4 changes: 2 additions & 2 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@

<protobuf.version>3.7.1</protobuf.version>
<grpc.version>1.17.1</grpc.version>
<kryo.version>4.0.2</kryo.version>
<kryo-serializers.version>0.42</kryo-serializers.version>
<kryo.version>5.3.0</kryo.version>
<kryo-serializers.version>0.45</kryo-serializers.version>
<hessian.version>4.0.63</hessian.version>
<fst.version>2.57</fst.version>
<groovy.version>2.4.4</groovy.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,50 @@
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
import com.esotericsoftware.kryo.util.Pool;
import de.javakaffee.kryoserializers.JdkProxySerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author jsbxyyx
*/
public class KryoSerializerFactory implements KryoFactory {
public class KryoSerializerFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(KryoSerializerFactory.class);

private static final KryoSerializerFactory FACTORY = new KryoSerializerFactory();

private KryoPool pool = new KryoPool.Builder(this).softReferences().build();
private Pool<Kryo> pool = new Pool<Kryo>(true, true) {

@Override
public Kryo create() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

for (Map.Entry<Class, Serializer> entry : TYPE_MAP.entrySet()) {
kryo.register(entry.getKey(), entry.getValue());
}

// support clob and blob
kryo.register(SerialBlob.class, new BlobSerializer());
kryo.register(SerialClob.class, new ClobSerializer());

// register sql type
kryo.register(Timestamp.class, new TimestampSerializer());
kryo.register(InvocationHandler.class, new JdkProxySerializer());
// register commonly class
UndoLogSerializerClassRegistry.getRegisteredClasses().forEach((clazz, ser) -> {
if (ser == null) {
kryo.register(clazz);
} else {
kryo.register(clazz, (Serializer)ser);
}
});
return kryo;
}

};

private static final Map<Class, Serializer> TYPE_MAP = new ConcurrentHashMap<>();

Expand All @@ -54,14 +82,14 @@ public static KryoSerializerFactory getInstance() {
}

public KryoSerializer get() {
return new KryoSerializer(pool.borrow());
return new KryoSerializer(pool.obtain());
}

public void returnKryo(KryoSerializer kryoSerializer) {
if (kryoSerializer == null) {
throw new IllegalArgumentException("kryoSerializer is null");
}
pool.release(kryoSerializer.getKryo());
pool.free(kryoSerializer.getKryo());
}

public void registerSerializer(Class type, Serializer ser) {
Expand All @@ -70,33 +98,6 @@ public void registerSerializer(Class type, Serializer ser) {
}
}

@Override
public Kryo create() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

for (Map.Entry<Class, Serializer> entry : TYPE_MAP.entrySet()) {
kryo.register(entry.getKey(), entry.getValue());
}

// support clob and blob
kryo.register(SerialBlob.class, new BlobSerializer());
kryo.register(SerialClob.class, new ClobSerializer());

// register sql type
kryo.register(Timestamp.class, new TimestampSerializer());
kryo.register(InvocationHandler.class, new JdkProxySerializer());
// register commonly class
UndoLogSerializerClassRegistry.getRegisteredClasses().forEach((clazz, ser) -> {
if (ser == null) {
kryo.register(clazz);
} else {
kryo.register(clazz, (Serializer)ser);
}
});
return kryo;
}

private static class BlobSerializer extends Serializer<Blob> {

@Override
Expand All @@ -111,7 +112,7 @@ public void write(Kryo kryo, Output output, Blob object) {
}

@Override
public Blob read(Kryo kryo, Input input, Class<Blob> type) {
public Blob read(Kryo kryo, Input input, Class<? extends Blob> type) {
int length = input.readInt(true);
byte[] bytes = input.readBytes(length);
try {
Expand All @@ -137,7 +138,7 @@ public void write(Kryo kryo, Output output, Clob object) {
}

@Override
public Clob read(Kryo kryo, Input input, Class<Clob> type) {
public Clob read(Kryo kryo, Input input, Class<? extends Clob> type) {
try {
String s = input.readString();
return new SerialClob(s.toCharArray());
Expand All @@ -157,7 +158,7 @@ public void write(Kryo kryo, Output output, Timestamp object) {
}

@Override
public Timestamp read(Kryo kryo, Input input, Class<Timestamp> type) {
public Timestamp read(Kryo kryo, Input input, Class<? extends Timestamp> type) {
Timestamp timestamp = new Timestamp(input.readLong(true));
timestamp.setNanos(input.readInt(true));
return timestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
import java.util.UUID;
import java.util.regex.Pattern;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
import com.esotericsoftware.kryo.serializers.DefaultSerializers;
import com.esotericsoftware.kryo.util.Pool;
import de.javakaffee.kryoserializers.ArraysAsListSerializer;
import de.javakaffee.kryoserializers.BitSetSerializer;
import de.javakaffee.kryoserializers.GregorianCalendarSerializer;
Expand All @@ -40,11 +39,33 @@
/**
* @author jsbxyyx
*/
public class KryoSerializerFactory implements KryoFactory {
public class KryoSerializerFactory {

private static final KryoSerializerFactory FACTORY = new KryoSerializerFactory();

private KryoPool pool = new KryoPool.Builder(this).softReferences().build();
private Pool<Kryo> pool = new Pool<Kryo>(true, true) {

@Override
protected Kryo create() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

// register serializer
kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer());
kryo.register(InvocationHandler.class, new JdkProxySerializer());
kryo.register(BigDecimal.class, new DefaultSerializers.BigDecimalSerializer());
kryo.register(BigInteger.class, new DefaultSerializers.BigIntegerSerializer());
kryo.register(Pattern.class, new RegexSerializer());
kryo.register(BitSet.class, new BitSetSerializer());
kryo.register(URI.class, new URISerializer());
kryo.register(UUID.class, new UUIDSerializer());

// register commonly class
SerializerClassRegistry.getRegisteredClasses().keySet().forEach(kryo::register);
return kryo;
}
};

private KryoSerializerFactory() {}

Expand All @@ -53,35 +74,14 @@ public static KryoSerializerFactory getInstance() {
}

public KryoInnerSerializer get() {
return new KryoInnerSerializer(pool.borrow());
return new KryoInnerSerializer(pool.obtain());
}

public void returnKryo(KryoInnerSerializer kryoSerializer) {
if (kryoSerializer == null) {
throw new IllegalArgumentException("kryoSerializer is null");
}
pool.release(kryoSerializer.getKryo());
}

@Override
public Kryo create() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

// register serializer
kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer());
kryo.register(InvocationHandler.class, new JdkProxySerializer());
kryo.register(BigDecimal.class, new DefaultSerializers.BigDecimalSerializer());
kryo.register(BigInteger.class, new DefaultSerializers.BigIntegerSerializer());
kryo.register(Pattern.class, new RegexSerializer());
kryo.register(BitSet.class, new BitSetSerializer());
kryo.register(URI.class, new URISerializer());
kryo.register(UUID.class, new UUIDSerializer());

// register commonly class
SerializerClassRegistry.getRegisteredClasses().keySet().forEach(kryo::register);
return kryo;
pool.free(kryoSerializer.getKryo());
}

}

0 comments on commit dac8aa8

Please sign in to comment.