Skip to content

Commit b2ba2b3

Browse files
committed
Register all classes and fix the unit test issues
Registration is required by default in Kryo5: https://github.com/EsotericSoftware/kryo/wiki/Migration-to-v5#configuration-changes
1 parent 4814075 commit b2ba2b3

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

chill-hadoop/src/test/scala/com/twitter/chill/hadoop/HadoopTests.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ import com.twitter.chill.KryoInstantiator
2727
import org.scalatest.matchers.should.Matchers
2828
import org.scalatest.wordspec.AnyWordSpec
2929

30+
31+
class AnyKryoInstantiator extends KryoInstantiator {
32+
override def newKryo: Kryo = {
33+
val k = new Kryo
34+
k.register(Class.forName("com.twitter.chill.hadoop.HadoopTests"))
35+
k.register(Class.forName("scala.collection.immutable.List"))
36+
k
37+
}
38+
}
3039
class StdKryoInstantiator extends KryoInstantiator {
3140
override def newKryo: Kryo = {
3241
val k = new Kryo
42+
k.register(Class.forName("scala.Tuple2$mcII$sp"))
3343
k.setInstantiatorStrategy(new StdInstantiatorStrategy)
3444
k
3545
}
@@ -55,7 +65,7 @@ class HadoopTests extends AnyWordSpec with Matchers {
5565
"accept anything" in {
5666
val conf = new Configuration
5767
val hc = new HadoopConfig(conf)
58-
ConfiguredInstantiator.setReflect(hc, classOf[KryoInstantiator])
68+
ConfiguredInstantiator.setReflect(hc, classOf[AnyKryoInstantiator])
5969

6070
val ks = new KryoSerialization(conf)
6171
Seq(classOf[List[_]], classOf[Int], this.getClass).forall { cls =>

chill-java/src/test/java/com/twitter/chill/java/TestCollections.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ public class TestCollections {
4343
test_map.put(4, "four");
4444
}
4545

46-
public static <T> T serializeAndDeserialize(T t) {
46+
public static <T> T serializeAndDeserialize(T t) throws ClassNotFoundException {
4747
Output output = new Output(1000, -1);
48+
kryo.register(Class.forName("java.util.ArrayList"));
49+
kryo.register(Class.forName("java.util.LinkedList"));
50+
kryo.register(Class.forName("java.util.HashMap"));
51+
kryo.register(Class.forName("java.util.TreeMap"));
52+
kryo.register(Class.forName("java.util.HashSet"));
53+
kryo.register(Class.forName("java.util.TreeSet"));
4854
kryo.writeClassAndObject(output, t);
4955
Input input = new Input(output.toBytes());
5056
return (T) kryo.readClassAndObject(input);

chill-java/src/test/java/com/twitter/chill/java/TestLists.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public class TestLists {
5151
));
5252
}
5353

54-
public static <T> T serializeAndDeserialize(T t) {
54+
public static <T> T serializeAndDeserialize(T t) throws ClassNotFoundException {
5555
Output output = new Output(1000, -1);
56+
kryo.register(Class.forName("java.util.List"));
5657
kryo.writeClassAndObject(output, t);
5758
Input input = new Input(output.toBytes());
5859
return (T) kryo.readClassAndObject(input);

chill-java/src/test/scala/com/twitter/chill/config/ConfiguredInstantiatorTest.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ import org.scalatest.wordspec.AnyWordSpec
2727
class TestInst extends KryoInstantiator { override def newKryo = new Kryo }
2828
class TestInstTwo extends KryoInstantiator { override def newKryo = new Kryo }
2929

30+
class DefaultKryoInstantiator extends KryoInstantiator {
31+
override def newKryo: Kryo = {
32+
val k = new Kryo
33+
k.setRegistrationRequired(false)
34+
k
35+
}
36+
}
37+
3038
class ReflectingInstantiatorTest extends AnyWordSpec with Matchers {
3139
"A ConfiguredInstantiator" should {
3240
"work with a reflected instantiator" in {
@@ -38,15 +46,15 @@ class ReflectingInstantiatorTest extends AnyWordSpec with Matchers {
3846
}
3947
"work with a serialized instantiator" in {
4048
val conf = new JavaMapConfig
41-
ConfiguredInstantiator.setSerialized(conf, new TestInst)
49+
ConfiguredInstantiator.setSerialized(conf, classOf[DefaultKryoInstantiator], new TestInst)
4250
val cci = new ConfiguredInstantiator(conf)
4351
// Here is the only assert:
4452
cci.getDelegate.getClass should equal(classOf[TestInst])
4553
// Verify that caching is working:
4654
val cci2 = new ConfiguredInstantiator(conf)
4755
cci.getDelegate should equal(cci2.getDelegate)
4856
// Set a new serialized and verify caching is still correct:
49-
ConfiguredInstantiator.setSerialized(conf, new TestInstTwo)
57+
ConfiguredInstantiator.setSerialized(conf, classOf[DefaultKryoInstantiator], new TestInstTwo)
5058
val cci3 = new ConfiguredInstantiator(conf)
5159
cci3.getDelegate.getClass should equal(classOf[TestInstTwo])
5260
(cci3.getDelegate should not).equal(cci2.getDelegate)

chill-java/src/test/scala/com/twitter/chill/java/PriorityQueueTest.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class PriorityQueueTest extends AnyWordSpec with Matchers {
3737
import scala.collection.JavaConverters._
3838

3939
val kryo = new Kryo()
40+
kryo.register(Class.forName("scala.math.Ordering$$anon$9"))
41+
kryo.register(
42+
Class.forName("com.twitter.chill.java.PriorityQueueTest$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$2")
43+
)
44+
kryo.register(Class.forName("scala.Tuple2$mcII$sp"))
4045
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy)
4146
PriorityQueueSerializer.registrar()(kryo)
4247
new Java8ClosureRegistrar()(kryo)

chill-protobuf/src/test/scala/com/twitter/chill/protobuf/ProtobufTest.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.twitter.chill.protobuf
1818

19+
import com.twitter.chill.java.UnmodifiableListSerializer
1920
import com.twitter.chill.{KryoInstantiator, KryoPool}
2021
import com.twitter.chill.protobuf.TestMessages.FatigueCount
2122

@@ -44,6 +45,14 @@ class ProtobufTest extends AnyWordSpec with Matchers {
4445
new KryoInstantiator {
4546
override def newKryo(): Kryo = {
4647
val k = new Kryo
48+
k.register(Class.forName("com.twitter.chill.protobuf.TestMessages$FatigueCount"))
49+
k.register(Class.forName("java.util.ArrayList"))
50+
k.register(
51+
Class.forName("java.util.Collections$UnmodifiableRandomAccessList"),
52+
new UnmodifiableListSerializer
53+
)
54+
k.register(Class.forName("com.google.protobuf.UnknownFieldSet"))
55+
k.register(Class.forName("java.util.Collections$EmptyMap"))
4756
k.addDefaultSerializer(classOf[Message], classOf[ProtobufSerializer])
4857
k
4958
}

0 commit comments

Comments
 (0)