Bug 2890: Chunk AppendEntries when single payload size exceeds threshold
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / ServerConfigurationPayload.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 com.google.common.collect.ImmutableList;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.ByteArrayOutputStream;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.ObjectOutputStream;
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
23 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Payload data for server configuration log entries.
29  *
30  * @author Thomas Pantelis
31  */
32 public final class ServerConfigurationPayload extends Payload implements PersistentPayload, Serializable {
33     private static final class Proxy implements Externalizable {
34         private static final long serialVersionUID = 1L;
35
36         private List<ServerInfo> serverConfig;
37
38         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
39         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
40         @SuppressWarnings("checkstyle:RedundantModifier")
41         public Proxy() {
42             // For Externalizable
43         }
44
45         Proxy(final ServerConfigurationPayload payload) {
46             this.serverConfig = payload.getServerConfig();
47         }
48
49         @Override
50         public void writeExternal(final ObjectOutput out) throws IOException {
51             out.writeInt(serverConfig.size());
52             for (ServerInfo i : serverConfig) {
53                 out.writeObject(i.getId());
54                 out.writeBoolean(i.isVoting());
55             }
56         }
57
58         @Override
59         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
60             final int size = in.readInt();
61             serverConfig = new ArrayList<>(size);
62             for (int i = 0; i < size; ++i) {
63                 final String id = (String) in.readObject();
64                 final boolean voting = in.readBoolean();
65                 serverConfig.add(new ServerInfo(id, voting));
66             }
67         }
68
69         private Object readResolve() {
70             return new ServerConfigurationPayload(serverConfig);
71         }
72     }
73
74     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
75     private static final long serialVersionUID = 1L;
76
77     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
78             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
79             + "aren't serialized. FindBugs does not recognize this.")
80     private final List<ServerInfo> serverConfig;
81     private int serializedSize = -1;
82
83     public ServerConfigurationPayload(@Nonnull final List<ServerInfo> serverConfig) {
84         this.serverConfig = ImmutableList.copyOf(serverConfig);
85     }
86
87     @Nonnull
88     public List<ServerInfo> getServerConfig() {
89         return serverConfig;
90     }
91
92     @Override
93     public int size() {
94         if (serializedSize < 0) {
95             try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
96                 try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
97                     out.writeObject(writeReplace());
98                 }
99
100                 serializedSize = bos.toByteArray().length;
101             } catch (IOException e) {
102                 serializedSize = 0;
103                 LOG.error("Error serializing", e);
104             }
105         }
106
107         return serializedSize;
108     }
109
110     @Override
111     public int hashCode() {
112         return serverConfig.hashCode();
113     }
114
115     @Override
116     public boolean equals(Object obj) {
117         if (this == obj) {
118             return true;
119         }
120
121         if (obj == null) {
122             return false;
123         }
124
125         if (getClass() != obj.getClass()) {
126             return false;
127         }
128
129         ServerConfigurationPayload other = (ServerConfigurationPayload) obj;
130         return serverConfig.equals(other.serverConfig);
131     }
132
133     @Override
134     public String toString() {
135         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
136     }
137
138     private Object writeReplace() {
139         return new Proxy(this);
140     }
141 }