Move {Identifiable,Persistent,}Payload
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / Snapshot.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 package org.opendaylight.controller.cluster.raft.persisted;
9
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.messages.Payload;
19
20 /**
21  * Represents a snapshot of the raft data.
22  *
23  * @author Thomas Pantelis
24  */
25 // Not final for mocking
26 public class Snapshot implements Serializable {
27
28     /**
29      * Implementations of this interface are used as the state payload for a snapshot.
30      *
31      * @author Thomas Pantelis
32      */
33     public interface State extends Serializable {
34         /**
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.
37          *
38          * @return True if complete recovery based upon this snapshot should trigger a new snapshot.
39          */
40         default boolean needsMigration() {
41             return false;
42         }
43     }
44
45     private static final class Proxy implements Externalizable {
46         private static final long serialVersionUID = 1L;
47
48         private Snapshot snapshot;
49
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")
53         public Proxy() {
54             // For Externalizable
55         }
56
57         Proxy(final Snapshot snapshot) {
58             this.snapshot = snapshot;
59         }
60
61         @Override
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);
70
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());
76             }
77
78             out.writeObject(snapshot.state);
79         }
80
81         @Override
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();
90
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()));
96             }
97
98             State state = (State) in.readObject();
99
100             snapshot = Snapshot.create(state, unAppliedEntries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
101                     electionTerm, electionVotedFor, serverConfig);
102         }
103
104         private Object readResolve() {
105             return snapshot;
106         }
107     }
108
109     private static final long serialVersionUID = 1L;
110
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;
120
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) {
124         this.state = state;
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;
133     }
134
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);
140     }
141
142     public State getState() {
143         return state;
144     }
145
146     public List<ReplicatedLogEntry> getUnAppliedEntries() {
147         return unAppliedEntries;
148     }
149
150     public long getLastTerm() {
151         return lastTerm;
152     }
153
154     public long getLastAppliedIndex() {
155         return lastAppliedIndex;
156     }
157
158     public long getLastAppliedTerm() {
159         return lastAppliedTerm;
160     }
161
162     public long getLastIndex() {
163         return this.lastIndex;
164     }
165
166     public long getElectionTerm() {
167         return electionTerm;
168     }
169
170     public String getElectionVotedFor() {
171         return electionVotedFor;
172     }
173
174     public ServerConfigurationPayload getServerConfiguration() {
175         return serverConfig;
176     }
177
178     private Object writeReplace() {
179         return new Proxy(this);
180     }
181
182     @Override
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 + "]";
188     }
189 }