Fix ReadyLocalTransactionSerializer 02/68402/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Jan 2018 12:22:08 +0000 (13:22 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 19 Feb 2018 11:38:44 +0000 (12:38 +0100)
The following exception was seen in the field:

2017-12-20 19:37:05,507 | ERROR | ult-dispatcher-2 | Remoting                         | 174 - com.typesafe.akka.slf4j - 2.4.7 | java.lang.ClassNotFoundException: org.opendaylight.controller.cluster.datastore.messages.BatchedModifications
org.apache.commons.lang3.SerializationException: java.lang.ClassNotFoundException: org.opendaylight.controller.cluster.datastore.messages.BatchedModifications
        at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:229)
        at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:267)
        at org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransactionSerializer.fromBinaryJava(ReadyLocalTransactionSerializer.java:49)
        at akka.serialization.JSerializer.fromBinary(Serializer.scala:177)
        at akka.serialization.Serialization$$anonfun$deserialize$2.apply(Serialization.scala:124)
        at scala.util.Try$.apply(Try.scala:192)
        at akka.serialization.Serialization.deserialize(Serialization.scala:114)
        at akka.remote.serialization.MessageContainerSerializer.fromBinary(MessageContainerSerializer.scala:80)
        at akka.serialization.Serialization$$anonfun$deserialize$2.apply(Serialization.scala:124)
        at scala.util.Try$.apply(Try.scala:192)
        at akka.serialization.Serialization.deserialize(Serialization.scala:114)
        at akka.remote.MessageSerializer$.deserialize(MessageSerializer.scala:24)
        at akka.remote.DefaultMessageDispatcher.payload$lzycompute$1(Endpoint.scala:60)
        at akka.remote.DefaultMessageDispatcher.payload$1(Endpoint.scala:60)
        at akka.remote.DefaultMessageDispatcher.dispatch(Endpoint.scala:78)
        at akka.remote.EndpointReader$$anonfun$receive$2.applyOrElse(Endpoint.scala:978)
        at akka.actor.Actor$class.aroundReceive(Actor.scala:484)
        at akka.remote.EndpointActor.aroundReceive(Endpoint.scala:447)
        at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
        at akka.actor.ActorCell.invoke(ActorCell.scala:495)
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
        at akka.dispatch.Mailbox.run(Mailbox.scala:224)
        at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.ClassNotFoundException: org.opendaylight.controller.cluster.datastore.messages.BatchedModifications
        at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
        at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
        at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:683)
        at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1863)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1746)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2037)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:428)
        at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:223)
        ... 26 more

As it turns out, ReadyLocalTransactionSerializer is not following JSerializer
documentation recommendations of loading classes via ExtendedActorSystem.

Change-Id: Idef62f8c7a50d607ef152083693fac63c7e92447
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadyLocalTransactionSerializer.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/messages/ReadyLocalTransactionSerializerTest.java

index d095cbd..60a85b9 100644 (file)
@@ -7,8 +7,12 @@
  */
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import akka.actor.ExtendedActorSystem;
 import akka.serialization.JSerializer;
+import akka.util.ClassLoaderObjectInputStream;
 import com.google.common.base.Preconditions;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import org.apache.commons.lang3.SerializationUtils;
 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
 
@@ -19,6 +23,13 @@ import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModifi
  * shards.
  */
 public final class ReadyLocalTransactionSerializer extends JSerializer {
+
+    private final ExtendedActorSystem system;
+
+    public ReadyLocalTransactionSerializer(final ExtendedActorSystem system) {
+        this.system = Preconditions.checkNotNull(system);
+    }
+
     @Override
     public int identifier() {
         return 97439437;
@@ -46,7 +57,12 @@ public final class ReadyLocalTransactionSerializer extends JSerializer {
 
     @Override
     public Object fromBinaryJava(final byte[] bytes, final Class<?> clazz) {
-        return SerializationUtils.deserialize(bytes);
+        try (ClassLoaderObjectInputStream is = new ClassLoaderObjectInputStream(system.dynamicAccess().classLoader(),
+            new ByteArrayInputStream(bytes))) {
+            return is.readObject();
+        } catch (IOException | ClassNotFoundException e) {
+            throw new IllegalStateException("Failed to deserialize object", e);
+        }
     }
 
     private static final class BatchedCursor extends AbstractBatchedModificationsCursor {
index cc5dbff..5a585bc 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.cluster.datastore.messages;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import akka.actor.ExtendedActorSystem;
+import akka.testkit.JavaTestKit;
 import java.io.NotSerializableException;
 import java.util.List;
 import org.junit.Test;
@@ -49,11 +51,15 @@ public class ReadyLocalTransactionSerializerTest extends AbstractTest {
         TransactionIdentifier txId = nextTransactionId();
         ReadyLocalTransaction readyMessage = new ReadyLocalTransaction(txId, modification, true);
 
-        ReadyLocalTransactionSerializer serializer = new ReadyLocalTransactionSerializer();
-
-        byte[] bytes = serializer.toBinary(readyMessage);
-
-        Object deserialized = serializer.fromBinary(bytes, ReadyLocalTransaction.class);
+        final ExtendedActorSystem system = (ExtendedActorSystem) ExtendedActorSystem.create("test");
+        final Object deserialized;
+        try {
+            final ReadyLocalTransactionSerializer serializer = new ReadyLocalTransactionSerializer(system);
+            final byte[] bytes = serializer.toBinary(readyMessage);
+            deserialized = serializer.fromBinary(bytes, ReadyLocalTransaction.class);
+        } finally {
+            JavaTestKit.shutdownActorSystem(system);
+        }
 
         assertNotNull("fromBinary returned null", deserialized);
         assertEquals("fromBinary return type", BatchedModifications.class, deserialized.getClass());

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.