Bug 5740: Change RequestVote(Reply) to externalizable proxy 94/58394/2
authorTom Pantelis <tompantelis@gmail.com>
Wed, 24 May 2017 12:02:00 +0000 (08:02 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 9 Jun 2017 00:45:54 +0000 (00:45 +0000)
The other RaftRPC classes havwe been converted to use the
externalizable proxy pattern so we shoild convert RequestVote(Reply)
as well.

Change-Id: I0a2054d8426f66480f37061d1a9fc51464f705da
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/RequestVote.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReply.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java [new file with mode: 0644]

index fc6d48906a73b63f34eaf053792b2c3a0cb1c3c0..5ed18f918a0d7ba87c7cf75e6567ee1548711c99 100644 (file)
@@ -8,6 +8,11 @@
 
 package org.opendaylight.controller.cluster.raft.messages;
 
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
 /**
  * Invoked by candidates to gather votes (ยง5.2).
  */
@@ -50,4 +55,46 @@ public class RequestVote extends AbstractRaftRPC {
                 .append("]");
         return builder.toString();
     }
+
+    private Object writeReplace() {
+        return new Proxy(this);
+    }
+
+    private static class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+
+        private RequestVote requestVote;
+
+        // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+        // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+        @SuppressWarnings("checkstyle:RedundantModifier")
+        public Proxy() {
+        }
+
+        Proxy(RequestVote requestVote) {
+            this.requestVote = requestVote;
+        }
+
+        @Override
+        public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeLong(requestVote.getTerm());
+            out.writeObject(requestVote.candidateId);
+            out.writeLong(requestVote.lastLogIndex);
+            out.writeLong(requestVote.lastLogTerm);
+        }
+
+        @Override
+        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+            long term = in.readLong();
+            String candidateId = (String) in.readObject();
+            long lastLogIndex = in.readLong();
+            long lastLogTerm = in.readLong();
+
+            requestVote = new RequestVote(term, candidateId, lastLogIndex, lastLogTerm);
+        }
+
+        private Object readResolve() {
+            return requestVote;
+        }
+    }
 }
index bbb1222effccbc5ee276bfc64dd6aff6adcefed7..af208ec85b70040327aaa96e7d0798a1e19375c6 100644 (file)
@@ -8,6 +8,11 @@
 
 package org.opendaylight.controller.cluster.raft.messages;
 
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
 public final class RequestVoteReply extends AbstractRaftRPC {
     private static final long serialVersionUID = 8427899326488775660L;
 
@@ -27,4 +32,42 @@ public final class RequestVoteReply extends AbstractRaftRPC {
     public String toString() {
         return "RequestVoteReply [term=" + getTerm() + ", voteGranted=" + voteGranted + "]";
     }
+
+    private Object writeReplace() {
+        return new Proxy(this);
+    }
+
+    private static class Proxy implements Externalizable {
+        private static final long serialVersionUID = 1L;
+
+        private RequestVoteReply requestVoteReply;
+
+        // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+        // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+        @SuppressWarnings("checkstyle:RedundantModifier")
+        public Proxy() {
+        }
+
+        Proxy(RequestVoteReply requestVoteReply) {
+            this.requestVoteReply = requestVoteReply;
+        }
+
+        @Override
+        public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeLong(requestVoteReply.getTerm());
+            out.writeBoolean(requestVoteReply.voteGranted);
+        }
+
+        @Override
+        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+            long term = in.readLong();
+            boolean voteGranted = in.readBoolean();
+
+            requestVoteReply = new RequestVoteReply(term, voteGranted);
+        }
+
+        private Object readResolve() {
+            return requestVoteReply;
+        }
+    }
 }
diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteReplyTest.java
new file mode 100644 (file)
index 0000000..fa1bb5f
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.messages;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Test;
+
+/**
+ * Unit tests for RequestVoteReply.
+ *
+ * @author Thomas Pantelis
+ */
+public class RequestVoteReplyTest {
+
+    @Test
+    public void testSerialization() {
+        RequestVoteReply expected = new RequestVoteReply(5, true);
+        RequestVoteReply cloned = (RequestVoteReply) SerializationUtils.clone(expected);
+
+        assertEquals("getTerm", expected.getTerm(), cloned.getTerm());
+        assertEquals("isVoteGranted", expected.isVoteGranted(), cloned.isVoteGranted());
+    }
+}
diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/messages/RequestVoteTest.java
new file mode 100644 (file)
index 0000000..6cb9179
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.messages;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Test;
+
+/**
+ * Unit tests for RequestVote.
+ *
+ * @author Thomas Pantelis
+ */
+public class RequestVoteTest {
+
+    @Test
+    public void testSerialization() {
+        RequestVote expected = new RequestVote(4, "candidateId", 3, 2);
+        RequestVote cloned = (RequestVote) SerializationUtils.clone(expected);
+
+        assertEquals("getTerm", expected.getTerm(), cloned.getTerm());
+        assertEquals("getCandidateId", expected.getCandidateId(), cloned.getCandidateId());
+        assertEquals("getLastLogIndex", expected.getLastLogIndex(), cloned.getLastLogIndex());
+        assertEquals("getLastLogTerm", expected.getLastLogTerm(), cloned.getLastLogTerm());
+    }
+}