Bug 5740: Change RequestVote(Reply) to externalizable proxy
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / RequestVote.java
index 8321d0c25bcecce32617c55fe652c2b7be189541..5ed18f918a0d7ba87c7cf75e6567ee1548711c99 100644 (file)
@@ -8,37 +8,33 @@
 
 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).
  */
 public class RequestVote extends AbstractRaftRPC {
-    private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = -6967509186297108657L;
 
     // candidate requesting vote
-    private String candidateId;
+    private final String candidateId;
 
     // index of candidate’s last log entry (§5.4)
-    private long lastLogIndex;
+    private final long lastLogIndex;
 
     // term of candidate’s last log entry (§5.4)
-    private long lastLogTerm;
+    private final long lastLogTerm;
 
-    public RequestVote(long term, String candidateId, long lastLogIndex,
-        long lastLogTerm) {
+    public RequestVote(long term, String candidateId, long lastLogIndex, long lastLogTerm) {
         super(term);
         this.candidateId = candidateId;
         this.lastLogIndex = lastLogIndex;
         this.lastLogTerm = lastLogTerm;
     }
 
-    // added for testing while serialize-messages=on
-    public RequestVote() {
-    }
-
-    public long getTerm() {
-        return term;
-    }
-
     public String getCandidateId() {
         return candidateId;
     }
@@ -51,26 +47,54 @@ public class RequestVote extends AbstractRaftRPC {
         return lastLogTerm;
     }
 
-    public void setCandidateId(String candidateId) {
-        this.candidateId = candidateId;
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append("RequestVote [term=").append(getTerm()).append(", candidateId=").append(candidateId)
+                .append(", lastLogIndex=").append(lastLogIndex).append(", lastLogTerm=").append(lastLogTerm)
+                .append("]");
+        return builder.toString();
     }
 
-    public void setLastLogIndex(long lastLogIndex) {
-        this.lastLogIndex = lastLogIndex;
+    private Object writeReplace() {
+        return new Proxy(this);
     }
 
-    public void setLastLogTerm(long lastLogTerm) {
-        this.lastLogTerm = lastLogTerm;
-    }
+    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);
+        }
 
-    @Override public String toString() {
-        final StringBuilder sb =
-            new StringBuilder("RequestVote{");
-        sb.append("term='").append(getTerm()).append('\'');
-        sb.append("candidateId='").append(candidateId).append('\'');
-        sb.append(", lastLogIndex=").append(lastLogIndex);
-        sb.append(", lastLogTerm=").append(lastLogTerm);
-        sb.append('}');
-        return sb.toString();
+        private Object readResolve() {
+            return requestVote;
+        }
     }
 }