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
9 package org.opendaylight.controller.cluster.raft.messages;
11 import com.google.common.base.Optional;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
19 * Message sent from a leader to install a snapshot chunk on a follower.
21 public class InstallSnapshot extends AbstractRaftRPC {
22 private static final long serialVersionUID = 1L;
24 private final String leaderId;
25 private final long lastIncludedIndex;
26 private final long lastIncludedTerm;
27 private final byte[] data;
28 private final int chunkIndex;
29 private final int totalChunks;
30 private final Optional<Integer> lastChunkHashCode;
31 private final Optional<ServerConfigurationPayload> serverConfig;
33 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex, long lastIncludedTerm, byte[] data,
34 int chunkIndex, int totalChunks, Optional<Integer> lastChunkHashCode,
35 Optional<ServerConfigurationPayload> serverConfig) {
37 this.leaderId = leaderId;
38 this.lastIncludedIndex = lastIncludedIndex;
39 this.lastIncludedTerm = lastIncludedTerm;
41 this.chunkIndex = chunkIndex;
42 this.totalChunks = totalChunks;
43 this.lastChunkHashCode = lastChunkHashCode;
44 this.serverConfig = serverConfig;
47 public InstallSnapshot(long term, String leaderId, long lastIncludedIndex,
48 long lastIncludedTerm, byte[] data, int chunkIndex, int totalChunks) {
49 this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks,
50 Optional.<Integer>absent(), Optional.<ServerConfigurationPayload>absent());
53 public String getLeaderId() {
57 public long getLastIncludedIndex() {
58 return lastIncludedIndex;
61 public long getLastIncludedTerm() {
62 return lastIncludedTerm;
65 public byte[] getData() {
69 public int getChunkIndex() {
73 public int getTotalChunks() {
77 public Optional<Integer> getLastChunkHashCode() {
78 return lastChunkHashCode;
81 public Optional<ServerConfigurationPayload> getServerConfig() {
86 public <T> Object toSerializable(short version) {
91 public String toString() {
92 return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
93 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
94 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
95 + ", serverConfig=" + serverConfig.orNull() + "]";
98 private Object writeReplace() {
99 return new Proxy(this);
102 private static class Proxy implements Externalizable {
103 private static final long serialVersionUID = 1L;
105 private InstallSnapshot installSnapshot;
107 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
108 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
109 @SuppressWarnings("checkstyle:RedundantModifier")
113 Proxy(InstallSnapshot installSnapshot) {
114 this.installSnapshot = installSnapshot;
118 public void writeExternal(ObjectOutput out) throws IOException {
119 out.writeLong(installSnapshot.getTerm());
120 out.writeObject(installSnapshot.leaderId);
121 out.writeLong(installSnapshot.lastIncludedIndex);
122 out.writeLong(installSnapshot.lastIncludedTerm);
123 out.writeInt(installSnapshot.chunkIndex);
124 out.writeInt(installSnapshot.totalChunks);
126 out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
127 if (installSnapshot.lastChunkHashCode.isPresent()) {
128 out.writeInt(installSnapshot.lastChunkHashCode.get().intValue());
131 out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
132 if (installSnapshot.serverConfig.isPresent()) {
133 out.writeObject(installSnapshot.serverConfig.get());
136 out.writeObject(installSnapshot.data);
140 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
141 long term = in.readLong();
142 String leaderId = (String) in.readObject();
143 long lastIncludedIndex = in.readLong();
144 long lastIncludedTerm = in.readLong();
145 int chunkIndex = in.readInt();
146 int totalChunks = in.readInt();
148 Optional<Integer> lastChunkHashCode = Optional.absent();
149 boolean chunkHashCodePresent = in.readByte() == 1;
150 if (chunkHashCodePresent) {
151 lastChunkHashCode = Optional.of(in.readInt());
154 Optional<ServerConfigurationPayload> serverConfig = Optional.absent();
155 boolean serverConfigPresent = in.readByte() == 1;
156 if (serverConfigPresent) {
157 serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
160 byte[] data = (byte[])in.readObject();
162 installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
163 chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
166 private Object readResolve() {
167 return installSnapshot;