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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.