Fixup "Leader should always apply modifications as local" regression
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / CommitTransactionPayload.java
1 /*
2  * Copyright (c) 2016 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.datastore.persisted;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.controller.cluster.datastore.persisted.ChunkedOutputStream.MAX_ARRAY_SIZE;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.io.ByteStreams;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.io.DataInput;
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.Externalizable;
22 import java.io.IOException;
23 import java.io.ObjectInput;
24 import java.io.ObjectOutput;
25 import java.io.Serializable;
26 import java.io.StreamCorruptedException;
27 import java.util.AbstractMap.SimpleImmutableEntry;
28 import java.util.Map.Entry;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
31 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateInputOutput.DataTreeCandidateWithVersion;
32 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.IdentifiablePayload;
33 import org.opendaylight.yangtools.concepts.Variant;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Payload persisted when a transaction commits. It contains the transaction identifier and the
42  * {@link DataTreeCandidate}
43  *
44  * @author Robert Varga
45  */
46 @Beta
47 public abstract class CommitTransactionPayload extends IdentifiablePayload<TransactionIdentifier>
48         implements Serializable {
49     private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
50     private static final long serialVersionUID = 1L;
51
52     private volatile Entry<TransactionIdentifier, DataTreeCandidateWithVersion> candidate = null;
53
54     CommitTransactionPayload() {
55
56     }
57
58     public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
59             final DataTreeCandidate candidate, final PayloadVersion version, final int initialSerializedBufferCapacity)
60                     throws IOException {
61         final ChunkedOutputStream cos = new ChunkedOutputStream(initialSerializedBufferCapacity);
62         try (DataOutputStream dos = new DataOutputStream(cos)) {
63             transactionId.writeTo(dos);
64             DataTreeCandidateInputOutput.writeDataTreeCandidate(dos, version, candidate);
65         }
66
67         final Variant<byte[], ChunkedByteArray> source = cos.toVariant();
68         LOG.debug("Initial buffer capacity {}, actual serialized size {}", initialSerializedBufferCapacity, cos.size());
69         return source.isFirst() ? new Simple(source.getFirst()) : new Chunked(source.getSecond());
70     }
71
72     @VisibleForTesting
73     public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
74             final DataTreeCandidate candidate, final PayloadVersion version) throws IOException {
75         return create(transactionId, candidate, version, 512);
76     }
77
78     @VisibleForTesting
79     public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
80             final DataTreeCandidate candidate) throws IOException {
81         return create(transactionId, candidate, PayloadVersion.current());
82     }
83
84     public @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate() throws IOException {
85         Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = candidate;
86         if (localCandidate == null) {
87             synchronized (this) {
88                 localCandidate = candidate;
89                 if (localCandidate == null) {
90                     candidate = localCandidate = getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
91                 }
92             }
93         }
94         return localCandidate;
95     }
96
97     public final @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate(
98             final ReusableStreamReceiver receiver) throws IOException {
99         final DataInput in = newDataInput();
100         return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
101                 DataTreeCandidateInputOutput.readDataTreeCandidate(in, receiver));
102     }
103
104     @Override
105     public TransactionIdentifier getIdentifier() {
106         try  {
107             return getCandidate().getKey();
108         } catch (IOException e) {
109             throw new IllegalStateException("Candidate deserialization failed.", e);
110         }
111     }
112
113     /**
114      * The cached candidate needs to be cleared after it is done applying to the DataTree, otherwise it would be keeping
115      * deserialized in memory which are not needed anymore leading to wasted memory. This lets the payload know that
116      * this was the last time the candidate was needed ant it is safe to be cleared.
117      */
118     public Entry<TransactionIdentifier, DataTreeCandidateWithVersion> acquireCandidate() throws IOException {
119         final Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = getCandidate();
120         candidate = null;
121         return localCandidate;
122     }
123
124     abstract void writeBytes(ObjectOutput out) throws IOException;
125
126     abstract DataInput newDataInput();
127
128     final Object writeReplace() {
129         return new Proxy(this);
130     }
131
132     private static final class Simple extends CommitTransactionPayload {
133         private static final long serialVersionUID = 1L;
134
135         private final byte[] serialized;
136
137         Simple(final byte[] serialized) {
138             this.serialized = requireNonNull(serialized);
139         }
140
141         @Override
142         public int size() {
143             return serialized.length;
144         }
145
146         @Override
147         DataInput newDataInput() {
148             return ByteStreams.newDataInput(serialized);
149         }
150
151         @Override
152         void writeBytes(final ObjectOutput out) throws IOException {
153             out.write(serialized);
154         }
155     }
156
157     private static final class Chunked extends CommitTransactionPayload {
158         private static final long serialVersionUID = 1L;
159
160         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via serialization proxy")
161         private final ChunkedByteArray source;
162
163         Chunked(final ChunkedByteArray source) {
164             this.source = requireNonNull(source);
165         }
166
167         @Override
168         void writeBytes(final ObjectOutput out) throws IOException {
169             source.copyTo(out);
170         }
171
172         @Override
173         public int size() {
174             return source.size();
175         }
176
177         @Override
178         DataInput newDataInput() {
179             return new DataInputStream(source.openStream());
180         }
181     }
182
183     private static final class Proxy implements Externalizable {
184         private static final long serialVersionUID = 1L;
185
186         private CommitTransactionPayload payload;
187
188         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
189         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
190         @SuppressWarnings("checkstyle:RedundantModifier")
191         public Proxy() {
192             // For Externalizable
193         }
194
195         Proxy(final CommitTransactionPayload payload) {
196             this.payload = requireNonNull(payload);
197         }
198
199         @Override
200         public void writeExternal(final ObjectOutput out) throws IOException {
201             out.writeInt(payload.size());
202             payload.writeBytes(out);
203         }
204
205         @Override
206         public void readExternal(final ObjectInput in) throws IOException {
207             final int length = in.readInt();
208             if (length < 0) {
209                 throw new StreamCorruptedException("Invalid payload length " + length);
210             } else if (length < MAX_ARRAY_SIZE) {
211                 final byte[] serialized = new byte[length];
212                 in.readFully(serialized);
213                 payload = new Simple(serialized);
214             } else {
215                 payload = new Chunked(ChunkedByteArray.readFrom(in, length, MAX_ARRAY_SIZE));
216             }
217         }
218
219         private Object readResolve() {
220             return verifyNotNull(payload);
221         }
222     }
223 }