Force AbstractRaftRPC to use Externalizable proxy pattern 65/83165/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 19 Jul 2019 07:15:15 +0000 (09:15 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 22 Jul 2019 09:37:43 +0000 (09:37 +0000)
All implementations are already using the pattern, this change
makes that official and removes the no-arg constructor, which
is not used anyway.

Change-Id: Iaea4a2f39972537907cce19551d2ca6e4b33fad6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/AbstractRaftRPC.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/AppendEntries.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/AppendEntriesReply.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshot.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/InstallSnapshotReply.java
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

index fc5255e..76edc54 100644 (file)
@@ -5,29 +5,23 @@
  * 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;
 
-public class AbstractRaftRPC implements RaftRPC {
+public abstract class AbstractRaftRPC implements RaftRPC {
     private static final long serialVersionUID = -6061342433962854822L;
 
     // term
-    private long term;
+    private final long term;
 
-    protected AbstractRaftRPC(long term) {
+    AbstractRaftRPC(final long term) {
         this.term = term;
     }
 
-    // added for testing while serialize-messages=on
-    public AbstractRaftRPC() {
-    }
-
     @Override
-    public long getTerm() {
+    public final long getTerm() {
         return term;
     }
 
-    protected void setTerm(long term) {
-        this.term = term;
-    }
+    // All implementations must use Externalizable Proxy pattern
+    abstract Object writeReplace();
 }
index 67c6899..6bdb7a4 100644 (file)
@@ -25,14 +25,13 @@ import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEnt
 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
 
 /**
- * Invoked by leader to replicate log entries (§5.3); also used as
- * heartbeat (§5.2).
+ * Invoked by leader to replicate log entries (§5.3); also used as heartbeat (§5.2).
  */
-public class AppendEntries extends AbstractRaftRPC {
+public final class AppendEntries extends AbstractRaftRPC {
     private static final long serialVersionUID = 1L;
 
     // So that follower can redirect clients
-    private final String leaderId;
+    private final @NonNull String leaderId;
 
     // Index of log entry immediately preceding new ones
     private final long prevLogIndex;
@@ -41,7 +40,7 @@ public class AppendEntries extends AbstractRaftRPC {
     private final long prevLogTerm;
 
     // log entries to store (empty for heart beat - may send more than one for efficiency)
-    private final List<ReplicatedLogEntry> entries;
+    private final @NonNull List<ReplicatedLogEntry> entries;
 
     // leader's commitIndex
     private final long leaderCommit;
@@ -57,9 +56,10 @@ public class AppendEntries extends AbstractRaftRPC {
 
     private final String leaderAddress;
 
-    private AppendEntries(long term, @NonNull String leaderId, long prevLogIndex, long prevLogTerm,
-            @NonNull List<ReplicatedLogEntry> entries, long leaderCommit, long replicatedToAllIndex,
-            short payloadVersion, short recipientRaftVersion, short leaderRaftVersion, @Nullable String leaderAddress) {
+    private AppendEntries(final long term, @NonNull final String leaderId, final long prevLogIndex,
+            final long prevLogTerm, @NonNull final List<ReplicatedLogEntry> entries, final long leaderCommit,
+            final long replicatedToAllIndex, final short payloadVersion, final short recipientRaftVersion,
+            final short leaderRaftVersion, @Nullable final String leaderAddress) {
         super(term);
         this.leaderId = requireNonNull(leaderId);
         this.prevLogIndex = prevLogIndex;
@@ -73,17 +73,18 @@ public class AppendEntries extends AbstractRaftRPC {
         this.leaderAddress = leaderAddress;
     }
 
-    public AppendEntries(long term, @NonNull String leaderId, long prevLogIndex, long prevLogTerm,
-            @NonNull List<ReplicatedLogEntry> entries, long leaderCommit, long replicatedToAllIndex,
-            short payloadVersion, short recipientRaftVersion, @Nullable String leaderAddress) {
+    public AppendEntries(final long term, final @NonNull String leaderId, final long prevLogIndex,
+            final long prevLogTerm, final @NonNull List<ReplicatedLogEntry> entries, final long leaderCommit,
+            final long replicatedToAllIndex, final short payloadVersion, final short recipientRaftVersion,
+            final @Nullable String leaderAddress) {
         this(term, leaderId, prevLogIndex, prevLogTerm, entries, leaderCommit, replicatedToAllIndex, payloadVersion,
                 recipientRaftVersion, RaftVersions.CURRENT_VERSION, leaderAddress);
     }
 
     @VisibleForTesting
-    public AppendEntries(long term, @NonNull String leaderId, long prevLogIndex, long prevLogTerm,
-            @NonNull List<ReplicatedLogEntry> entries, long leaderCommit, long replicatedToAllIndex,
-            short payloadVersion) {
+    public AppendEntries(final long term, final @NonNull String leaderId, final long prevLogIndex,
+            final long prevLogTerm, final @NonNull List<ReplicatedLogEntry> entries, final long leaderCommit,
+            final long replicatedToAllIndex, final short payloadVersion) {
         this(term, leaderId, prevLogIndex, prevLogTerm, entries, leaderCommit, replicatedToAllIndex, payloadVersion,
                 RaftVersions.CURRENT_VERSION, null);
     }
@@ -138,7 +139,8 @@ public class AppendEntries extends AbstractRaftRPC {
                 + ", entries=" + entries + "]";
     }
 
-    private Object writeReplace() {
+    @Override
+    Object writeReplace() {
         return recipientRaftVersion >= RaftVersions.FLUORINE_VERSION ? new ProxyV2(this) : new Proxy(this);
     }
 
@@ -156,12 +158,12 @@ public class AppendEntries extends AbstractRaftRPC {
         public ProxyV2() {
         }
 
-        ProxyV2(AppendEntries appendEntries) {
+        ProxyV2(final AppendEntries appendEntries) {
             this.appendEntries = appendEntries;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeShort(appendEntries.leaderRaftVersion);
             out.writeLong(appendEntries.getTerm());
             out.writeObject(appendEntries.leaderId);
@@ -182,7 +184,7 @@ public class AppendEntries extends AbstractRaftRPC {
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             short leaderRaftVersion = in.readShort();
             long term = in.readLong();
             String leaderId = (String) in.readObject();
@@ -225,12 +227,12 @@ public class AppendEntries extends AbstractRaftRPC {
         public Proxy() {
         }
 
-        Proxy(AppendEntries appendEntries) {
+        Proxy(final AppendEntries appendEntries) {
             this.appendEntries = appendEntries;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeLong(appendEntries.getTerm());
             out.writeObject(appendEntries.leaderId);
             out.writeLong(appendEntries.prevLogTerm);
@@ -248,7 +250,7 @@ public class AppendEntries extends AbstractRaftRPC {
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             long term = in.readLong();
             String leaderId = (String) in.readObject();
             long prevLogTerm = in.readLong();
index 902b9a0..ef24697 100644 (file)
@@ -5,7 +5,6 @@
  * 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 com.google.common.annotations.VisibleForTesting;
@@ -18,7 +17,7 @@ import org.opendaylight.controller.cluster.raft.RaftVersions;
 /**
  * Reply for the AppendEntries message.
  */
-public class AppendEntriesReply extends AbstractRaftRPC {
+public final class AppendEntriesReply extends AbstractRaftRPC {
     private static final long serialVersionUID = -7487547356392536683L;
 
     // true if follower contained entry matching
@@ -47,23 +46,22 @@ public class AppendEntriesReply extends AbstractRaftRPC {
     private final short recipientRaftVersion;
 
     @VisibleForTesting
-    public AppendEntriesReply(String followerId, long term, boolean success, long logLastIndex, long logLastTerm,
-            short payloadVersion) {
+    public AppendEntriesReply(final String followerId, final long term, final boolean success, final long logLastIndex,
+            final long logLastTerm, final short payloadVersion) {
         this(followerId, term, success, logLastIndex, logLastTerm, payloadVersion, false, false,
                 RaftVersions.CURRENT_VERSION);
     }
 
-    public AppendEntriesReply(String followerId, long term, boolean success, long logLastIndex, long logLastTerm,
-            short payloadVersion, boolean forceInstallSnapshot, boolean needsLeaderAddress,
-            short recipientRaftVersion) {
+    public AppendEntriesReply(final String followerId, final long term, final boolean success, final long logLastIndex,
+            final long logLastTerm, final short payloadVersion, final boolean forceInstallSnapshot,
+            final boolean needsLeaderAddress, final short recipientRaftVersion) {
         this(followerId, term, success, logLastIndex, logLastTerm, payloadVersion, forceInstallSnapshot,
                 needsLeaderAddress, RaftVersions.CURRENT_VERSION, recipientRaftVersion);
-
     }
 
-    private AppendEntriesReply(String followerId, long term, boolean success, long logLastIndex, long logLastTerm,
-            short payloadVersion, boolean forceInstallSnapshot, boolean needsLeaderAddress, short raftVersion,
-            short recipientRaftVersion) {
+    private AppendEntriesReply(final String followerId, final long term, final boolean success, final long logLastIndex,
+            final long logLastTerm, final short payloadVersion, final boolean forceInstallSnapshot,
+            final boolean needsLeaderAddress, final short raftVersion, final short recipientRaftVersion) {
         super(term);
         this.followerId = followerId;
         this.success = success;
@@ -117,7 +115,8 @@ public class AppendEntriesReply extends AbstractRaftRPC {
                 + ", recipientRaftVersion=" + recipientRaftVersion + "]";
     }
 
-    private Object writeReplace() {
+    @Override
+    Object writeReplace() {
         return recipientRaftVersion >= RaftVersions.FLUORINE_VERSION ? new Proxy2(this) : new Proxy(this);
     }
 
@@ -135,12 +134,12 @@ public class AppendEntriesReply extends AbstractRaftRPC {
         public Proxy2() {
         }
 
-        Proxy2(AppendEntriesReply appendEntriesReply) {
+        Proxy2(final AppendEntriesReply appendEntriesReply) {
             this.appendEntriesReply = appendEntriesReply;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeShort(appendEntriesReply.raftVersion);
             out.writeLong(appendEntriesReply.getTerm());
             out.writeObject(appendEntriesReply.followerId);
@@ -153,7 +152,7 @@ public class AppendEntriesReply extends AbstractRaftRPC {
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             short raftVersion = in.readShort();
             long term = in.readLong();
             String followerId = (String) in.readObject();
@@ -189,12 +188,12 @@ public class AppendEntriesReply extends AbstractRaftRPC {
         public Proxy() {
         }
 
-        Proxy(AppendEntriesReply appendEntriesReply) {
+        Proxy(final AppendEntriesReply appendEntriesReply) {
             this.appendEntriesReply = appendEntriesReply;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeShort(appendEntriesReply.raftVersion);
             out.writeLong(appendEntriesReply.getTerm());
             out.writeObject(appendEntriesReply.followerId);
@@ -206,7 +205,7 @@ public class AppendEntriesReply extends AbstractRaftRPC {
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             short raftVersion = in.readShort();
             long term = in.readLong();
             String followerId = (String) in.readObject();
index 39fa74b..60c54f7 100644 (file)
@@ -19,7 +19,7 @@ import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPay
 /**
  * Message sent from a leader to install a snapshot chunk on a follower.
  */
-public class InstallSnapshot extends AbstractRaftRPC {
+public final class InstallSnapshot extends AbstractRaftRPC {
     private static final long serialVersionUID = 1L;
 
     private final String leaderId;
@@ -92,7 +92,6 @@ public class InstallSnapshot extends AbstractRaftRPC {
         return serverConfig;
     }
 
-
     public <T> Object toSerializable(final short version) {
         return this;
     }
@@ -105,7 +104,8 @@ public class InstallSnapshot extends AbstractRaftRPC {
                 + ", serverConfig=" + serverConfig.orElse(null) + "]";
     }
 
-    private Object writeReplace() {
+    @Override
+    Object writeReplace() {
         return new Proxy(this);
     }
 
index bbee5c4..693fe7e 100644 (file)
@@ -5,7 +5,6 @@
  * 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 java.io.Externalizable;
@@ -13,7 +12,7 @@ import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 
-public class InstallSnapshotReply extends AbstractRaftRPC {
+public final class InstallSnapshotReply extends AbstractRaftRPC {
     private static final long serialVersionUID = 642227896390779503L;
 
     // The followerId - this will be used to figure out which follower is
@@ -22,7 +21,7 @@ public class InstallSnapshotReply extends AbstractRaftRPC {
     private final int chunkIndex;
     private final boolean success;
 
-    public InstallSnapshotReply(long term, String followerId, int chunkIndex, boolean success) {
+    public InstallSnapshotReply(final long term, final String followerId, final int chunkIndex, final boolean success) {
         super(term);
         this.followerId = followerId;
         this.chunkIndex = chunkIndex;
@@ -49,7 +48,8 @@ public class InstallSnapshotReply extends AbstractRaftRPC {
                 + ", success=" + success + "]";
     }
 
-    private Object writeReplace() {
+    @Override
+    Object writeReplace() {
         return new Proxy(this);
     }
 
@@ -64,12 +64,12 @@ public class InstallSnapshotReply extends AbstractRaftRPC {
         public Proxy() {
         }
 
-        Proxy(InstallSnapshotReply installSnapshotReply) {
+        Proxy(final InstallSnapshotReply installSnapshotReply) {
             this.installSnapshotReply = installSnapshotReply;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeLong(installSnapshotReply.getTerm());
             out.writeObject(installSnapshotReply.followerId);
             out.writeInt(installSnapshotReply.chunkIndex);
@@ -77,7 +77,7 @@ public class InstallSnapshotReply extends AbstractRaftRPC {
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             long term = in.readLong();
             String followerId = (String) in.readObject();
             int chunkIndex = in.readInt();
index d5a581a..b23c76d 100644 (file)
@@ -5,7 +5,6 @@
  * 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 java.io.Externalizable;
@@ -16,7 +15,7 @@ import java.io.ObjectOutput;
 /**
  * Invoked by candidates to gather votes (§5.2).
  */
-public class RequestVote extends AbstractRaftRPC {
+public final class RequestVote extends AbstractRaftRPC {
     private static final long serialVersionUID = -6967509186297108657L;
 
     // candidate requesting vote
@@ -28,7 +27,7 @@ public class RequestVote extends AbstractRaftRPC {
     // term of candidate’s last log entry (§5.4)
     private final long lastLogTerm;
 
-    public RequestVote(long term, String candidateId, long lastLogIndex, long lastLogTerm) {
+    public RequestVote(final long term, final String candidateId, final long lastLogIndex, final long lastLogTerm) {
         super(term);
         this.candidateId = candidateId;
         this.lastLogIndex = lastLogIndex;
@@ -56,7 +55,8 @@ public class RequestVote extends AbstractRaftRPC {
                 + "]";
     }
 
-    private Object writeReplace() {
+    @Override
+    Object writeReplace() {
         return new Proxy(this);
     }
 
@@ -71,12 +71,12 @@ public class RequestVote extends AbstractRaftRPC {
         public Proxy() {
         }
 
-        Proxy(RequestVote requestVote) {
+        Proxy(final RequestVote requestVote) {
             this.requestVote = requestVote;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeLong(requestVote.getTerm());
             out.writeObject(requestVote.candidateId);
             out.writeLong(requestVote.lastLogIndex);
@@ -84,7 +84,7 @@ public class RequestVote extends AbstractRaftRPC {
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
             long term = in.readLong();
             String candidateId = (String) in.readObject();
             long lastLogIndex = in.readLong();
index 9b7a2f3..2554c17 100644 (file)
@@ -5,7 +5,6 @@
  * 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 java.io.Externalizable;
@@ -19,7 +18,7 @@ public final class RequestVoteReply extends AbstractRaftRPC {
     // true means candidate received vote
     private final boolean voteGranted;
 
-    public RequestVoteReply(long term, boolean voteGranted) {
+    public RequestVoteReply(final long term, final boolean voteGranted) {
         super(term);
         this.voteGranted = voteGranted;
     }
@@ -33,7 +32,8 @@ public final class RequestVoteReply extends AbstractRaftRPC {
         return "RequestVoteReply [term=" + getTerm() + ", voteGranted=" + voteGranted + "]";
     }
 
-    private Object writeReplace() {
+    @Override
+    Object writeReplace() {
         return new Proxy(this);
     }
 
@@ -48,18 +48,18 @@ public final class RequestVoteReply extends AbstractRaftRPC {
         public Proxy() {
         }
 
-        Proxy(RequestVoteReply requestVoteReply) {
+        Proxy(final RequestVoteReply requestVoteReply) {
             this.requestVoteReply = requestVoteReply;
         }
 
         @Override
-        public void writeExternal(ObjectOutput out) throws IOException {
+        public void writeExternal(final ObjectOutput out) throws IOException {
             out.writeLong(requestVoteReply.getTerm());
             out.writeBoolean(requestVoteReply.voteGranted);
         }
 
         @Override
-        public void readExternal(ObjectInput in) throws IOException {
+        public void readExternal(final ObjectInput in) throws IOException {
             long term = in.readLong();
             boolean voteGranted = in.readBoolean();
 

©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.