2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft.persisted;
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import java.io.Serializable;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 * Represents a snapshot of the raft data.
23 * @author Thomas Pantelis
25 // Not final for mocking
26 public class Snapshot implements Serializable {
29 * Implementations of this interface are used as the state payload for a snapshot.
31 * @author Thomas Pantelis
33 public interface State extends Serializable {
35 * Indicate whether the snapshot requires migration, i.e. a new snapshot should be created after recovery.
36 * Default implementation returns false, i.e. do not re-snapshot.
38 * @return True if complete recovery based upon this snapshot should trigger a new snapshot.
40 default boolean needsMigration() {
45 private static final class Proxy implements Externalizable {
46 private static final long serialVersionUID = 1L;
48 private Snapshot snapshot;
50 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
51 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
52 @SuppressWarnings("checkstyle:RedundantModifier")
57 Proxy(final Snapshot snapshot) {
58 this.snapshot = snapshot;
62 public void writeExternal(final ObjectOutput out) throws IOException {
63 out.writeLong(snapshot.lastIndex);
64 out.writeLong(snapshot.lastTerm);
65 out.writeLong(snapshot.lastAppliedIndex);
66 out.writeLong(snapshot.lastAppliedTerm);
67 out.writeLong(snapshot.electionTerm);
68 out.writeObject(snapshot.electionVotedFor);
69 out.writeObject(snapshot.serverConfig);
71 out.writeInt(snapshot.unAppliedEntries.size());
72 for (ReplicatedLogEntry e: snapshot.unAppliedEntries) {
73 out.writeLong(e.getIndex());
74 out.writeLong(e.getTerm());
75 out.writeObject(e.getData());
78 out.writeObject(snapshot.state);
82 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
83 long lastIndex = in.readLong();
84 long lastTerm = in.readLong();
85 long lastAppliedIndex = in.readLong();
86 long lastAppliedTerm = in.readLong();
87 long electionTerm = in.readLong();
88 String electionVotedFor = (String) in.readObject();
89 ServerConfigurationPayload serverConfig = (ServerConfigurationPayload) in.readObject();
91 int size = in.readInt();
92 List<ReplicatedLogEntry> unAppliedEntries = new ArrayList<>(size);
93 for (int i = 0; i < size; i++) {
94 unAppliedEntries.add(new SimpleReplicatedLogEntry(in.readLong(), in.readLong(),
95 (Payload) in.readObject()));
98 State state = (State) in.readObject();
100 snapshot = Snapshot.create(state, unAppliedEntries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
101 electionTerm, electionVotedFor, serverConfig);
104 private Object readResolve() {
109 private static final long serialVersionUID = 1L;
111 private final State state;
112 private final List<ReplicatedLogEntry> unAppliedEntries;
113 private final long lastIndex;
114 private final long lastTerm;
115 private final long lastAppliedIndex;
116 private final long lastAppliedTerm;
117 private final long electionTerm;
118 private final String electionVotedFor;
119 private final ServerConfigurationPayload serverConfig;
121 Snapshot(final State state, final List<ReplicatedLogEntry> unAppliedEntries, final long lastIndex,
122 final long lastTerm, final long lastAppliedIndex, final long lastAppliedTerm, final long electionTerm,
123 final String electionVotedFor, final ServerConfigurationPayload serverConfig) {
125 this.unAppliedEntries = unAppliedEntries;
126 this.lastIndex = lastIndex;
127 this.lastTerm = lastTerm;
128 this.lastAppliedIndex = lastAppliedIndex;
129 this.lastAppliedTerm = lastAppliedTerm;
130 this.electionTerm = electionTerm;
131 this.electionVotedFor = electionVotedFor;
132 this.serverConfig = serverConfig;
135 public static Snapshot create(final State state, final List<ReplicatedLogEntry> entries, final long lastIndex,
136 final long lastTerm, final long lastAppliedIndex, final long lastAppliedTerm, final long electionTerm,
137 final String electionVotedFor, final ServerConfigurationPayload serverConfig) {
138 return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
139 electionTerm, electionVotedFor, serverConfig);
142 public State getState() {
146 public List<ReplicatedLogEntry> getUnAppliedEntries() {
147 return unAppliedEntries;
150 public long getLastTerm() {
154 public long getLastAppliedIndex() {
155 return lastAppliedIndex;
158 public long getLastAppliedTerm() {
159 return lastAppliedTerm;
162 public long getLastIndex() {
163 return this.lastIndex;
166 public long getElectionTerm() {
170 public String getElectionVotedFor() {
171 return electionVotedFor;
174 public ServerConfigurationPayload getServerConfiguration() {
178 private Object writeReplace() {
179 return new Proxy(this);
183 public String toString() {
184 return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
185 + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
186 + ", state=" + state + ", electionTerm=" + electionTerm + ", electionVotedFor="
187 + electionVotedFor + ", ServerConfigPayload=" + serverConfig + "]";