Add optional timeout parameter for backup rpc
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / InstallSnapshot.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.messages;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.Optional;
16 import java.util.OptionalInt;
17 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
18
19 /**
20  * Message sent from a leader to install a snapshot chunk on a follower.
21  */
22 public final class InstallSnapshot extends AbstractRaftRPC {
23     private static final long serialVersionUID = 1L;
24
25     private final String leaderId;
26     private final long lastIncludedIndex;
27     private final long lastIncludedTerm;
28     private final byte[] data;
29     private final int chunkIndex;
30     private final int totalChunks;
31     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
32     private final OptionalInt lastChunkHashCode;
33     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via writeReplace()")
34     private final Optional<ServerConfigurationPayload> serverConfig;
35
36     @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Stores a reference to an externally mutable byte[] "
37             + "object but this is OK since this class is merely a DTO and does not process byte[] internally. "
38             + "Also it would be inefficient to create a copy as the byte[] could be large.")
39     public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
40             final long lastIncludedTerm, final byte[] data, final int chunkIndex, final int totalChunks,
41             final OptionalInt lastChunkHashCode, final Optional<ServerConfigurationPayload> serverConfig) {
42         super(term);
43         this.leaderId = leaderId;
44         this.lastIncludedIndex = lastIncludedIndex;
45         this.lastIncludedTerm = lastIncludedTerm;
46         this.data = data;
47         this.chunkIndex = chunkIndex;
48         this.totalChunks = totalChunks;
49         this.lastChunkHashCode = lastChunkHashCode;
50         this.serverConfig = serverConfig;
51     }
52
53     public InstallSnapshot(final long term, final String leaderId, final long lastIncludedIndex,
54                            final long lastIncludedTerm, final byte[] data, final int chunkIndex,
55                            final int totalChunks) {
56         this(term, leaderId, lastIncludedIndex, lastIncludedTerm, data, chunkIndex, totalChunks, OptionalInt.empty(),
57             Optional.empty());
58     }
59
60     public String getLeaderId() {
61         return leaderId;
62     }
63
64     public long getLastIncludedIndex() {
65         return lastIncludedIndex;
66     }
67
68     public long getLastIncludedTerm() {
69         return lastIncludedTerm;
70     }
71
72     @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
73             + "this is OK since this class is merely a DTO and does not process the byte[] internally. "
74             + "Also it would be inefficient to create a return copy as the byte[] could be large.")
75     public byte[] getData() {
76         return data;
77     }
78
79     public int getChunkIndex() {
80         return chunkIndex;
81     }
82
83     public int getTotalChunks() {
84         return totalChunks;
85     }
86
87     public OptionalInt getLastChunkHashCode() {
88         return lastChunkHashCode;
89     }
90
91     public Optional<ServerConfigurationPayload> getServerConfig() {
92         return serverConfig;
93     }
94
95     public <T> Object toSerializable(final short version) {
96         return this;
97     }
98
99     @Override
100     public String toString() {
101         return "InstallSnapshot [term=" + getTerm() + ", leaderId=" + leaderId + ", lastIncludedIndex="
102                 + lastIncludedIndex + ", lastIncludedTerm=" + lastIncludedTerm + ", datasize=" + data.length
103                 + ", Chunk=" + chunkIndex + "/" + totalChunks + ", lastChunkHashCode=" + lastChunkHashCode
104                 + ", serverConfig=" + serverConfig.orElse(null) + "]";
105     }
106
107     @Override
108     Object writeReplace() {
109         return new Proxy(this);
110     }
111
112     private static class Proxy implements Externalizable {
113         private static final long serialVersionUID = 1L;
114
115         private InstallSnapshot installSnapshot;
116
117         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
118         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
119         @SuppressWarnings("checkstyle:RedundantModifier")
120         public Proxy() {
121         }
122
123         Proxy(final InstallSnapshot installSnapshot) {
124             this.installSnapshot = installSnapshot;
125         }
126
127         @Override
128         public void writeExternal(final ObjectOutput out) throws IOException {
129             out.writeLong(installSnapshot.getTerm());
130             out.writeObject(installSnapshot.leaderId);
131             out.writeLong(installSnapshot.lastIncludedIndex);
132             out.writeLong(installSnapshot.lastIncludedTerm);
133             out.writeInt(installSnapshot.chunkIndex);
134             out.writeInt(installSnapshot.totalChunks);
135
136             out.writeByte(installSnapshot.lastChunkHashCode.isPresent() ? 1 : 0);
137             if (installSnapshot.lastChunkHashCode.isPresent()) {
138                 out.writeInt(installSnapshot.lastChunkHashCode.getAsInt());
139             }
140
141             out.writeByte(installSnapshot.serverConfig.isPresent() ? 1 : 0);
142             if (installSnapshot.serverConfig.isPresent()) {
143                 out.writeObject(installSnapshot.serverConfig.get());
144             }
145
146             out.writeObject(installSnapshot.data);
147         }
148
149         @Override
150         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
151             long term = in.readLong();
152             String leaderId = (String) in.readObject();
153             long lastIncludedIndex = in.readLong();
154             long lastIncludedTerm = in.readLong();
155             int chunkIndex = in.readInt();
156             int totalChunks = in.readInt();
157
158             OptionalInt lastChunkHashCode = in.readByte() == 1 ? OptionalInt.of(in.readInt()) : OptionalInt.empty();
159             Optional<ServerConfigurationPayload> serverConfig = in.readByte() == 1
160                     ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
161
162             byte[] data = (byte[])in.readObject();
163
164             installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
165                     chunkIndex, totalChunks, lastChunkHashCode, serverConfig);
166         }
167
168         private Object readResolve() {
169             return installSnapshot;
170         }
171     }
172 }