0

I am using kryo for serialization with Scala 2.11.1 and Hzaelcast 3.5 i am trying to put my data in hazelcast map but i am getting KryoException

here is my model class

  @SerialVersionUID(1)
        case class User( id : Int ,name : String, userType : UserType /*UserType is Enum (EMPLOYED , UNEMPLOYED)*/ , userhistory : UserHistory) extends  Serializable{

def this()= {
this(0,"",Active, null)
}
}

here is UserHistory class

    @SerialVersionUID(1)
case  class UserHistory( date : DateTime = DateTime.now(), artworkStatus : ArtworkStatus = ACTIVE) extends  Serializable{

def this()={
    this(DateTime.parse("0"),ACTIVE) 
  }
}

here is my serializer for User class

class UserSerializer extends StreamSerializer[User] {


   val log = LoggerFactory.getLogger(this.getClass)

   override def destroy() {

  }

  override def getTypeId() : Int ={
    val value : Int = 1;
    value

  }

  // takes the bytes and converts into User Object
   @throws(classOf[IOException])
  override def read(in : ObjectDataInput) : User =  {

     val kryo = new Kryo 
     val input = new Input(in.asInstanceOf[InputStream])
      log.info("********** Reading the bytes and converting into User object")
     kryo.readClassAndObject(input).asInstanceOf[User]



  }

   // takes User Object and converts into bytes
  @throws(classOf[IOException])
  override def write(out : ObjectDataOutput, obj : User) {

    val kryo= new Kryo
    val bops = new ByteArrayOutputStream
    val output = new Output(bops)
    kryo.writeClassAndObject(output , obj)
    output.flush
    output.close

    bops.writeTo(out.asInstanceOf[OutputStream])

    log.info("********** User object writen into bytes successfully")


  }


}

now when I put User class object from Hcast client into the respective map like this

map.set(user.id , user)

it gives me these exception:

 Cannot invoke the action, eventually got an error: com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val

Complete StackTraces from Hcast Client :

7:52:25.094 152121 [play-akka.actor.default-dispatcher-2] UserSerializer INFO - ********** User object writen into bytes successfully 17:52:29.667 156694 [play-akka.actor.default-dispatcher-2] play ERROR - Cannot invoke the action, eventually got an error: com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User) 17:52:29.832 156859 [play-akka.actor.default-dispatcher-2] application ERROR -

! @6nb1hbglj - Internal server error, for (POST) [/user/signup] ->

play.api.Application$$anon$1: Execution exception[[HazelcastSerializationException: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User) at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8] at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8] at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8] at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]

following are the StackTraces from Hcast Server

17:52:28.010 [hz._hzInstance_1_dev.partition-operation.thread-0] INFO models.serializers.UserSerializer - ********** Reading the bytes and converts into User object 17:52:28.990 [hz._hzInstance_1_dev.partition-operation.thread-0] ERROR c.h.map.impl.operation.SetOperation - [192.168.15.20]:5701 [dev] [3.5] com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User) com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User) at com.hazelcast.nio.serialization.SerializationServiceImpl.handleException(SerializationServiceImpl.java:380) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:282) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.map.impl.mapstore.AbstractMapDataStore.toObject(AbstractMapDataStore.java:78) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:39) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:31) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.map.impl.DefaultRecordStore.set(DefaultRecordStore.java:803) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.map.impl.operation.SetOperation.run(SetOperation.java:41) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:137) ~[hazelcast-3.5.jar:3.5] at com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.processOperation(OperationThread.java:154) [hazelcast-3.5.jar:3.5] at com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.process(OperationThread.java:110) [hazelcast-3.5.jar:3.5] at com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.doRun(OperationThread.java:101) [hazelcast-3.5.jar:3.5] at com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.run(OperationThread.java:76) [hazelcast-3.5.jar:3.5] Caused by: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val

Please Help me!!

M.Ahsen Taqi
  • 965
  • 11
  • 35

2 Answers2

1

Usually with Scala, you should use the StdInstantiatorStrategy, e.g.

val kryo = new Kryo
val is = new org.objenesis.strategy.StdInstantiatorStrategy
kryo.setInstantiatorStrategy(instantiatorStrategy)

Not sure if that will fix your issue, but I haven't seen any such problems.

nilskp
  • 3,097
  • 1
  • 30
  • 34
0

Did you register enum serializer as mentioned on github ?

        // Serialization of Scala enumerations
        kryo.addDefaultSerializer(classOf[scala.Enumeration#Value], classOf[EnumerationSerializer])
        kryo.register(Class.forName("scala.Enumeration$Val"))
        kryo.register(classOf[scala.Enumeration#Value])

Exception is pretty clear that it can not create userType which is enum.

com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User)
Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45
  • should i write serializer separatly for my enum (EnumerationSerializer in your solution)? – M.Ahsen Taqi Sep 02 '15 at 09:21
  • I'm not expert at kryo library but default Enum serializer should be ok for your custom UserType enum just explicitly register it like in the answer. – Fatih Donmez Sep 02 '15 at 11:20