Skip to content

Commit df4574d

Browse files
author
Sam Ritchie
committed
Merge branch 'release/0.3.2'
Conflicts: CHANGES.md README.md project/Build.scala
2 parents 9d77aef + 438e33c commit df4574d

File tree

15 files changed

+531
-13
lines changed

15 files changed

+531
-13
lines changed

CHANGES.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# chill #
22

3+
### 0.3.2
4+
5+
* Add a LocaleSerializer to chill-java: https://github.com/twitter/chill/pull/128
6+
* chill-akka module: https://github.com/twitter/chill/pull/116
7+
* Community section to README (with a mailing list!) https://github.com/twitter/chill/pull/127
8+
* SingletonSerializer for (): https://github.com/twitter/chill/pull/130
9+
* Add Externalizer (fault-tolerant meatlocker): https://github.com/twitter/chill/pull/126
10+
* Use References in Externalizer: https://github.com/twitter/chill/pull/131
11+
312
### 0.3.1
13+
414
* Issue 115: fix for wrapped Arrays with non-primitives
515

616
### 0.3.0

README.md

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Chill [![Build Status](https://secure.travis-ci.org/twitter/chill.png)](http://travis-ci.org/twitter/chill)
22

33
Extensions for the [Kryo serialization library](http://code.google.com/p/kryo/) including
4-
serializers and a set of classes to ease configuration of Kryo in systems like Hadoop, Storm,
4+
serializers and a set of classes to ease configuration of Kryo in systems like Hadoop, Storm,
55
Akka, etc.
66

77
Chill has a set of subprojects: chill-java, chill-hadoop, chill-storm and chill-scala. Other than
@@ -101,11 +101,8 @@ only scala serializers). Chill provides support for singletons, scala Objects a
101101
* all 22 scala tuples
102102

103103
## Chill-bijection
104-
[Bijections and Injections](https://github.com/twitter/bijection)
105-
are useful when considering serialization. If you have an Injection from
106-
`T` to `Array[Byte]` you have a serialization. Additionally, if you have a Bijection between `A`
107-
and `B`, and a serialization for `B`, then you have a serialization for `A`. See
108-
`BijectionEnrichedKryo` for easy interop between bijection and chill.
104+
105+
[Bijections and Injections](https://github.com/twitter/bijection) are useful when considering serialization. If you have an Injection from `T` to `Array[Byte]` you have a serialization. Additionally, if you have a Bijection between `A` and `B`, and a serialization for `B`, then you have a serialization for `A`. See `BijectionEnrichedKryo` for easy interop between bijection and chill.
109106

110107
### KryoInjection: easy serialization to byte Arrays
111108

@@ -120,9 +117,37 @@ val tryDecode: scala.util.Try[Any] = KryoInjection.invert(bytes)
120117

121118
KryoInjection can be composed with Bijections and Injections from `com.twitter.bijection`.
122119

120+
## Chill-Akka
121+
122+
To use, add a key to your config like:
123+
```
124+
akka.actor.serializers {
125+
kryo = "com.twitter.chill.akka.AkkaSerializer"
126+
}
127+
```
128+
129+
Then for the super-classes of all your message types, for instance, scala.Product, write:
130+
```scala
131+
akka.actor.serialization-bindings {
132+
"scala.Product" = kryo
133+
}
134+
```
135+
136+
If you want to use the `chill.config.ConfiguredInstantiator` see `ConfiguredAkkaSerializer`
137+
otherwise, subclass `AkkaSerializer` and override `kryoInstantiator` to control how the `Kryo`
138+
object is created.
139+
140+
## Community and Documentation
141+
142+
To learn more and find links to tutorials and information around the web, check out the [Chill Wiki](https://github.com/twitter/chill/wiki).
143+
144+
The latest ScalaDocs are hosted on Chill's [Github Project Page](http://twitter.github.io/chill).
145+
146+
Discussion occurs primarily on the [Chill mailing list](https://groups.google.com/forum/#!forum/chill-user). Issues should be reported on the [GitHub issue tracker](https://github.com/twitter/chill/issues).
147+
123148
## Maven
124149

125-
Chill modules are available on Maven Central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.3.1`.
150+
Chill modules are available on Maven Central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.3.2`.
126151

127152
Current published artifacts are
128153

@@ -133,6 +158,8 @@ Current published artifacts are
133158
* `chill_2.10`
134159
* `chill-bijection_2.9.3`
135160
* `chill-bijection_2.10`
161+
* `chill-akka_2.9.3`
162+
* `chill-akka_2.10`
136163

137164
The suffix denotes the scala version.
138165

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.twitter.chill.akka
2+
/*******************************************************************************
3+
* Copyright 2012 Roman Levenstein
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
******************************************************************************/
17+
18+
import akka.actor.ExtendedActorSystem
19+
import akka.actor.ActorRef
20+
import akka.serialization.Serialization
21+
import com.esotericsoftware.kryo.Kryo
22+
import com.esotericsoftware.kryo.Serializer
23+
import com.esotericsoftware.kryo.io.Input
24+
import com.esotericsoftware.kryo.io.Output
25+
26+
/***
27+
* This module provides helper classes for serialization of Akka-specific classes.
28+
*
29+
* @author Roman Levenstein
30+
* @author P. Oscar Boykin
31+
*/
32+
33+
import com.twitter.chill.{toRich, IKryoRegistrar}
34+
35+
class ActorRefSerializer(system: ExtendedActorSystem) extends Serializer[ActorRef] with IKryoRegistrar {
36+
37+
def apply(kryo: Kryo): Unit = {
38+
if(!kryo.alreadyRegistered(classOf[ActorRef])) {
39+
kryo.forClass[ActorRef](this)
40+
kryo.forSubclass[ActorRef](this)
41+
}
42+
}
43+
44+
override def read(kryo: Kryo, input: Input, typ: Class[ActorRef]): ActorRef = {
45+
val path = input.readString()
46+
system.actorFor(path)
47+
}
48+
49+
override def write(kryo: Kryo, output: Output, obj: ActorRef) = {
50+
Serialization.currentTransportAddress.value match {
51+
case null => output.writeString(obj.path.toString)
52+
case addr => output.writeString(obj.path.toStringWithAddress(addr))
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2013 Twitter, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.twitter.chill.akka
18+
19+
import com.twitter.chill.config.{Config => ChillConfig}
20+
import com.typesafe.config.{Config => TypesafeConfig}
21+
import com.typesafe.config.ConfigFactory
22+
23+
import scala.util.Try
24+
25+
/** Wraps the immutable typesafe.config.Config in a wrapper that
26+
* keeps track of the state and follows the semantics of ChillConfig
27+
*/
28+
class AkkaConfig(var typesafeConfig: TypesafeConfig) extends ChillConfig {
29+
/* This is implementing a Java API so that has an assy format */
30+
def get(key: String) =
31+
Try(typesafeConfig.getString(key)).toOption.orNull
32+
33+
def set(key: String, value: String) {
34+
typesafeConfig = Option(value).map { v =>
35+
ConfigFactory.parseString("%s = \"%s\"".format(key, v))
36+
.withFallback(typesafeConfig)
37+
}
38+
.getOrElse(typesafeConfig.withoutPath(key))
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2013 Twitter, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.twitter.chill.akka
18+
19+
import akka.actor.{ExtendedActorSystem, ActorRef}
20+
import akka.serialization.Serializer
21+
22+
import com.twitter.chill._
23+
import com.twitter.chill.config.ConfiguredInstantiator
24+
25+
/**
26+
* To use, add a key to your config like:
27+
*
28+
* {{{
29+
*
30+
* akka.actor.serializers {
31+
* kryo = "com.twitter.chill.akka.AkkaSerializer"
32+
* }
33+
* }}}
34+
*
35+
* Then for the super-classes of all your message types,
36+
* for instance, scala.Product, write:
37+
* {{{
38+
* akka.actor.serialization-bindings {
39+
* "scala.Product" = kryo
40+
* }
41+
* }}}
42+
*
43+
* Kryo is not thread-safe so we use an object pool to avoid over allocating.
44+
*/
45+
class AkkaSerializer(system: ExtendedActorSystem) extends Serializer {
46+
47+
/** You can override this to easily change your serializers.
48+
* If you do so, make sure to change the config to use the name of
49+
* your new class
50+
*/
51+
def kryoInstantiator: KryoInstantiator =
52+
(new ScalaKryoInstantiator).withRegistrar(new ActorRefSerializer(system))
53+
54+
/**
55+
* Since each thread only needs 1 Kryo, the pool doesn't need more
56+
* space than the number of threads. We guess that there are 4 hyperthreads /
57+
* core and then multiple by the nember of cores.
58+
*/
59+
def poolSize: Int = {
60+
val GUESS_THREADS_PER_CORE = 4
61+
GUESS_THREADS_PER_CORE * Runtime.getRuntime.availableProcessors
62+
}
63+
64+
val kryoPool: KryoPool =
65+
KryoPool.withByteArrayOutputStream(poolSize, kryoInstantiator)
66+
67+
def includeManifest: Boolean = false
68+
def identifier = 8675309
69+
def toBinary(obj: AnyRef): Array[Byte] = kryoPool.toBytesWithClass(obj)
70+
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef =
71+
kryoPool.fromBytes(bytes)
72+
}
73+
74+
/** Uses the Config system of chill.config to Configure at runtime which KryoInstantiator to use
75+
* Overriding kryoInstantiator and using your own class name is probably easier for most cases.
76+
* See ConfiguredInstantiator static methods for how to build up a correct Config with
77+
* your reflected or serialized instantiators.
78+
*/
79+
class ConfiguredAkkaSerializer(system: ExtendedActorSystem) extends AkkaSerializer(system) {
80+
override def kryoInstantiator: KryoInstantiator =
81+
(new ConfiguredInstantiator(new AkkaConfig(system.settings.config)))
82+
.withRegistrar(new ActorRefSerializer(system))
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2012 Twitter, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.twitter.chill.akka
18+
19+
import org.specs._
20+
21+
import akka.actor.{ ActorRef, ActorSystem }
22+
import akka.serialization._
23+
import com.typesafe.config.ConfigFactory
24+
25+
class AkkaTests extends Specification {
26+
27+
noDetailedDiffs() //Fixes issue for scala 2.9
28+
29+
val system = ActorSystem("example", ConfigFactory.parseString("""
30+
akka.actor.serializers {
31+
kryo = "com.twitter.chill.akka.AkkaSerializer"
32+
}
33+
34+
akka.actor.serialization-bindings {
35+
"scala.Product" = kryo
36+
"akka.actor.ActorRef" = kryo
37+
}
38+
"""))
39+
40+
// Get the Serialization Extension
41+
val serialization = SerializationExtension(system)
42+
43+
"AkkaSerializer" should {
44+
"be selected for tuples" in {
45+
// Find the Serializer for it
46+
val serializer = serialization.findSerializerFor((1,2,3))
47+
serializer.getClass.equals(classOf[AkkaSerializer]) must beTrue
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2013 Twitter, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.twitter.chill.java;
18+
19+
import com.esotericsoftware.kryo.Kryo;
20+
import com.esotericsoftware.kryo.serializers.JavaSerializer;
21+
import com.esotericsoftware.kryo.Serializer;
22+
import com.esotericsoftware.kryo.io.Input;
23+
import com.esotericsoftware.kryo.io.Output;
24+
25+
import com.twitter.chill.IKryoRegistrar;
26+
import com.twitter.chill.SingleRegistrar;
27+
28+
import java.util.Locale;
29+
30+
/** The java serializer uses an cache of allocated instances so
31+
* it is probably a bit hard to beat, so why bother
32+
*/
33+
public class LocaleSerializer extends JavaSerializer {
34+
static public IKryoRegistrar registrar() {
35+
return new SingleRegistrar(Locale.class, new LocaleSerializer());
36+
}
37+
}

chill-java/src/main/java/com/twitter/chill/java/PackageRegistrar.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static public IKryoRegistrar all() {
3838
TimestampSerializer.registrar(),
3939
URISerializer.registrar(),
4040
InetSocketAddressSerializer.registrar(),
41-
UUIDSerializer.registrar());
41+
UUIDSerializer.registrar(),
42+
LocaleSerializer.registrar(),
43+
SimpleDateFormatSerializer.registrar());
4244
}
4345
}

0 commit comments

Comments
 (0)