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;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Serializable;
12 import java.util.List;
13 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
16 * Represents a snapshot of the raft data.
19 * @author Thomas Pantelis
21 public class Snapshot implements Serializable {
22 private static final long serialVersionUID = -8298574936724056236L;
24 private final byte[] state;
25 private final List<ReplicatedLogEntry> unAppliedEntries;
26 private final long lastIndex;
27 private final long lastTerm;
28 private final long lastAppliedIndex;
29 private final long lastAppliedTerm;
30 private final long electionTerm;
31 private final String electionVotedFor;
32 private final ServerConfigurationPayload serverConfig;
34 private Snapshot(byte[] state, List<ReplicatedLogEntry> unAppliedEntries, long lastIndex, long lastTerm,
35 long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
36 ServerConfigurationPayload serverConfig) {
38 this.unAppliedEntries = unAppliedEntries;
39 this.lastIndex = lastIndex;
40 this.lastTerm = lastTerm;
41 this.lastAppliedIndex = lastAppliedIndex;
42 this.lastAppliedTerm = lastAppliedTerm;
43 this.electionTerm = electionTerm;
44 this.electionVotedFor = electionVotedFor;
45 this.serverConfig = serverConfig;
48 public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
49 long lastAppliedIndex, long lastAppliedTerm) {
50 return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm, -1, null, null);
53 public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
54 long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor) {
55 return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
56 electionTerm, electionVotedFor, null);
59 public static Snapshot create(byte[] state, List<ReplicatedLogEntry> entries, long lastIndex, long lastTerm,
60 long lastAppliedIndex, long lastAppliedTerm, long electionTerm, String electionVotedFor,
61 ServerConfigurationPayload serverConfig) {
62 return new Snapshot(state, entries, lastIndex, lastTerm, lastAppliedIndex, lastAppliedTerm,
63 electionTerm, electionVotedFor, serverConfig);
66 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
67 + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
68 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
69 public byte[] getState() {
73 public List<ReplicatedLogEntry> getUnAppliedEntries() {
74 return unAppliedEntries;
77 public long getLastTerm() {
81 public long getLastAppliedIndex() {
82 return lastAppliedIndex;
85 public long getLastAppliedTerm() {
86 return lastAppliedTerm;
89 public long getLastIndex() {
90 return this.lastIndex;
93 public long getElectionTerm() {
98 public String getElectionVotedFor() {
99 return electionVotedFor;
102 public ServerConfigurationPayload getServerConfiguration() {
107 public String toString() {
108 return "Snapshot [lastIndex=" + lastIndex + ", lastTerm=" + lastTerm + ", lastAppliedIndex=" + lastAppliedIndex
109 + ", lastAppliedTerm=" + lastAppliedTerm + ", unAppliedEntries size=" + unAppliedEntries.size()
110 + ", state size=" + state.length + ", electionTerm=" + electionTerm + ", electionVotedFor="
111 + electionVotedFor + ", ServerConfigPayload=" + serverConfig + "]";