Skip to content
Snippets Groups Projects
Commit f4b924f6 authored by Reynold Xin's avatar Reynold Xin
Browse files

Merge pull request #335 from rxin/ser

Fall back to zero-arg constructor for Serializer initialization if there is no constructor that accepts SparkConf.

This maintains backward compatibility with older serializers implemented by users.
parents d43ad3ef 63f90632
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,9 @@ import org.apache.spark.util.{NextIterator, ByteBufferInputStream}
* A serializer. Because some serialization libraries are not thread safe, this class is used to
* create [[org.apache.spark.serializer.SerializerInstance]] objects that do the actual serialization and are
* guaranteed to only be called from one thread at a time.
*
* Implementations of this trait should have a zero-arg constructor or a constructor that accepts a
* [[org.apache.spark.SparkConf]] as parameter. If both constructors are defined, the latter takes precedence.
*/
trait Serializer {
def newInstance(): SerializerInstance
......
......@@ -27,6 +27,7 @@ import org.apache.spark.SparkConf
* creating a new one.
*/
private[spark] class SerializerManager {
// TODO: Consider moving this into SparkConf itself to remove the global singleton.
private val serializers = new ConcurrentHashMap[String, Serializer]
private var _default: Serializer = _
......@@ -53,8 +54,18 @@ private[spark] class SerializerManager {
if (serializer == null) {
val clsLoader = Thread.currentThread.getContextClassLoader
val cls = Class.forName(clsName, true, clsLoader)
val constructor = cls.getConstructor(classOf[SparkConf])
serializer = constructor.newInstance(conf).asInstanceOf[Serializer]
// First try with the constructor that takes SparkConf. If we can't find one,
// use a no-arg constructor instead.
try {
val constructor = cls.getConstructor(classOf[SparkConf])
serializer = constructor.newInstance(conf).asInstanceOf[Serializer]
} catch {
case _: NoSuchMethodException =>
val constructor = cls.getConstructor()
serializer = constructor.newInstance().asInstanceOf[Serializer]
}
serializers.put(clsName, serializer)
}
serializer
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment