Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SimpleReplicatedLogEntry.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 static java.util.Objects.requireNonNull;
11
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.io.Serializable;
17 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
19
20 /**
21  * A {@link ReplicatedLogEntry} implementation.
22  *
23  * @author Thomas Pantelis
24  */
25 public final class SimpleReplicatedLogEntry implements ReplicatedLogEntry, Serializable {
26     private static final class Proxy implements Externalizable {
27         private static final long serialVersionUID = 1L;
28
29         private ReplicatedLogEntry replicatedLogEntry;
30
31         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
32         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
33         @SuppressWarnings("checkstyle:RedundantModifier")
34         public Proxy() {
35             // For Externalizable
36         }
37
38         Proxy(final ReplicatedLogEntry replicatedLogEntry) {
39             this.replicatedLogEntry = replicatedLogEntry;
40         }
41
42         static int estimatedSerializedSize(final ReplicatedLogEntry replicatedLogEntry) {
43             return 8 /* index */ + 8 /* term */ + replicatedLogEntry.getData().size()
44                     + 400 /* estimated extra padding for class info */;
45         }
46
47         @Override
48         public void writeExternal(final ObjectOutput out) throws IOException {
49             out.writeLong(replicatedLogEntry.getIndex());
50             out.writeLong(replicatedLogEntry.getTerm());
51             out.writeObject(replicatedLogEntry.getData());
52         }
53
54         @Override
55         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
56             replicatedLogEntry = new SimpleReplicatedLogEntry(in.readLong(), in.readLong(), (Payload) in.readObject());
57         }
58
59         private Object readResolve() {
60             return replicatedLogEntry;
61         }
62     }
63
64     private static final long serialVersionUID = 1L;
65
66     private final long index;
67     private final long term;
68     private final Payload payload;
69     private boolean persistencePending;
70
71     /**
72      * Constructs an instance.
73      *
74      * @param index the index
75      * @param term the term
76      * @param payload the payload
77      */
78     public SimpleReplicatedLogEntry(final long index, final long term, final Payload payload) {
79         this.index = index;
80         this.term = term;
81         this.payload = requireNonNull(payload);
82     }
83
84     @Override
85     public Payload getData() {
86         return payload;
87     }
88
89     @Override
90     public long getTerm() {
91         return term;
92     }
93
94     @Override
95     public long getIndex() {
96         return index;
97     }
98
99     @Override
100     public int size() {
101         return getData().size();
102     }
103
104     @Override
105     public boolean isPersistencePending() {
106         return persistencePending;
107     }
108
109     @Override
110     public void setPersistencePending(final boolean pending) {
111         persistencePending = pending;
112     }
113
114     private Object writeReplace() {
115         return new Proxy(this);
116     }
117
118     public int estimatedSerializedSize() {
119         return Proxy.estimatedSerializedSize(this);
120     }
121
122     @Override
123     public int hashCode() {
124         final int prime = 31;
125         int result = 1;
126         result = prime * result + payload.hashCode();
127         result = prime * result + (int) (index ^ index >>> 32);
128         result = prime * result + (int) (term ^ term >>> 32);
129         return result;
130     }
131
132     @Override
133     public boolean equals(final Object obj) {
134         if (this == obj) {
135             return true;
136         }
137
138         if (obj == null || getClass() != obj.getClass()) {
139             return false;
140         }
141
142         SimpleReplicatedLogEntry other = (SimpleReplicatedLogEntry) obj;
143         return index == other.index && term == other.term && payload.equals(other.payload);
144     }
145
146     @Override
147     public String toString() {
148         return "SimpleReplicatedLogEntry [index=" + index + ", term=" + term + ", payload=" + payload + "]";
149     }
150 }