d878e2c9c3cab8ba6105a97650ebc9e815c70c46
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / AppendEntries.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.raft.messages;
10
11 import com.google.common.base.Preconditions;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.ArrayList;
17 import java.util.List;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
20 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
21 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
22
23 /**
24  * Invoked by leader to replicate log entries (§5.3); also used as
25  * heartbeat (§5.2).
26  */
27 public class AppendEntries extends AbstractRaftRPC {
28     private static final long serialVersionUID = 1L;
29
30     // So that follower can redirect clients
31     private final String leaderId;
32
33     // Index of log entry immediately preceding new ones
34     private final long prevLogIndex;
35
36     // term of prevLogIndex entry
37     private final long prevLogTerm;
38
39     // log entries to store (empty for heart beat - may send more than one for efficiency)
40     private final List<ReplicatedLogEntry> entries;
41
42     // leader's commitIndex
43     private final long leaderCommit;
44
45     // index which has been replicated successfully to all followers, -1 if none
46     private final long replicatedToAllIndex;
47
48     private final short payloadVersion;
49
50     public AppendEntries(long term, @Nonnull String leaderId, long prevLogIndex, long prevLogTerm,
51             @Nonnull List<ReplicatedLogEntry> entries, long leaderCommit, long replicatedToAllIndex,
52             short payloadVersion) {
53         super(term);
54         this.leaderId = Preconditions.checkNotNull(leaderId);
55         this.prevLogIndex = prevLogIndex;
56         this.prevLogTerm = prevLogTerm;
57         this.entries = Preconditions.checkNotNull(entries);
58         this.leaderCommit = leaderCommit;
59         this.replicatedToAllIndex = replicatedToAllIndex;
60         this.payloadVersion = payloadVersion;
61     }
62
63     @Nonnull
64     public String getLeaderId() {
65         return leaderId;
66     }
67
68     public long getPrevLogIndex() {
69         return prevLogIndex;
70     }
71
72     public long getPrevLogTerm() {
73         return prevLogTerm;
74     }
75
76     @Nonnull
77     public List<ReplicatedLogEntry> getEntries() {
78         return entries;
79     }
80
81     public long getLeaderCommit() {
82         return leaderCommit;
83     }
84
85     public long getReplicatedToAllIndex() {
86         return replicatedToAllIndex;
87     }
88
89     public short getPayloadVersion() {
90         return payloadVersion;
91     }
92
93     @Override
94     public String toString() {
95         StringBuilder builder = new StringBuilder();
96         builder.append("AppendEntries [leaderId=").append(leaderId).append(", prevLogIndex=").append(prevLogIndex)
97                 .append(", prevLogTerm=").append(prevLogTerm).append(", leaderCommit=").append(leaderCommit)
98                 .append(", replicatedToAllIndex=").append(replicatedToAllIndex).append(", payloadVersion=")
99                 .append(payloadVersion).append(", entries=").append(entries).append("]");
100         return builder.toString();
101     }
102
103     private Object writeReplace() {
104         return new Proxy(this);
105     }
106
107     private static class Proxy implements Externalizable {
108         private static final long serialVersionUID = 1L;
109
110         private AppendEntries appendEntries;
111
112         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
113         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
114         @SuppressWarnings("checkstyle:RedundantModifier")
115         public Proxy() {
116         }
117
118         Proxy(AppendEntries appendEntries) {
119             this.appendEntries = appendEntries;
120         }
121
122         @Override
123         public void writeExternal(ObjectOutput out) throws IOException {
124             out.writeLong(appendEntries.getTerm());
125             out.writeObject(appendEntries.leaderId);
126             out.writeLong(appendEntries.prevLogTerm);
127             out.writeLong(appendEntries.prevLogIndex);
128             out.writeLong(appendEntries.leaderCommit);
129             out.writeLong(appendEntries.replicatedToAllIndex);
130             out.writeShort(appendEntries.payloadVersion);
131
132             out.writeInt(appendEntries.entries.size());
133             for (ReplicatedLogEntry e: appendEntries.entries) {
134                 out.writeLong(e.getIndex());
135                 out.writeLong(e.getTerm());
136                 out.writeObject(e.getData());
137             }
138         }
139
140         @Override
141         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
142             long term = in.readLong();
143             String leaderId = (String) in.readObject();
144             long prevLogTerm = in.readLong();
145             long prevLogIndex = in.readLong();
146             long leaderCommit = in.readLong();
147             long replicatedToAllIndex = in.readLong();
148             short payloadVersion = in.readShort();
149
150             int size = in.readInt();
151             List<ReplicatedLogEntry> entries = new ArrayList<>(size);
152             for (int i = 0; i < size; i++) {
153                 entries.add(new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject()));
154             }
155
156             appendEntries = new AppendEntries(term, leaderId, prevLogIndex, prevLogTerm, entries, leaderCommit,
157                     replicatedToAllIndex, payloadVersion);
158         }
159
160         private Object readResolve() {
161             return appendEntries;
162         }
163     }
164 }

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