2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
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;
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.Either;
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;
43 * Payload persisted when a transaction commits. It contains the transaction identifier and the
44 * {@link DataTreeCandidate}
46 * @author Robert Varga
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;
54 private static final int MAX_ARRAY_SIZE = ceilingPowerOfTwo(Integer.getInteger(
55 "org.opendaylight.controller.cluster.datastore.persisted.max-array-size", 256 * 1024));
57 private volatile Entry<TransactionIdentifier, DataTreeCandidateWithVersion> candidate = null;
59 CommitTransactionPayload() {
63 public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
64 final DataTreeCandidate candidate, final PayloadVersion version, final int initialSerializedBufferCapacity)
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);
72 final Either<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());
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);
84 public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
85 final DataTreeCandidate candidate) throws IOException {
86 return create(transactionId, candidate, PayloadVersion.current());
89 public @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate() throws IOException {
90 Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = candidate;
91 if (localCandidate == null) {
93 localCandidate = candidate;
94 if (localCandidate == null) {
95 candidate = localCandidate = getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
99 return localCandidate;
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));
110 public TransactionIdentifier getIdentifier() {
112 return getCandidate().getKey();
113 } catch (IOException e) {
114 throw new IllegalStateException("Candidate deserialization failed.", e);
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.
123 public Entry<TransactionIdentifier, DataTreeCandidateWithVersion> acquireCandidate() throws IOException {
124 final Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = getCandidate();
126 return localCandidate;
129 abstract void writeBytes(ObjectOutput out) throws IOException;
131 abstract DataInput newDataInput();
133 final Object writeReplace() {
134 return new Proxy(this);
137 private static final class Simple extends CommitTransactionPayload {
138 private static final long serialVersionUID = 1L;
140 private final byte[] serialized;
142 Simple(final byte[] serialized) {
143 this.serialized = requireNonNull(serialized);
148 return serialized.length;
152 DataInput newDataInput() {
153 return ByteStreams.newDataInput(serialized);
157 void writeBytes(final ObjectOutput out) throws IOException {
158 out.write(serialized);
162 private static final class Chunked extends CommitTransactionPayload {
163 private static final long serialVersionUID = 1L;
165 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via serialization proxy")
166 private final ChunkedByteArray source;
168 Chunked(final ChunkedByteArray source) {
169 this.source = requireNonNull(source);
173 void writeBytes(final ObjectOutput out) throws IOException {
179 return source.size();
183 DataInput newDataInput() {
184 return new DataInputStream(source.openStream());
188 private static final class Proxy implements Externalizable {
189 private static final long serialVersionUID = 1L;
191 private CommitTransactionPayload payload;
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")
197 // For Externalizable
200 Proxy(final CommitTransactionPayload payload) {
201 this.payload = requireNonNull(payload);
205 public void writeExternal(final ObjectOutput out) throws IOException {
206 out.writeInt(payload.size());
207 payload.writeBytes(out);
211 public void readExternal(final ObjectInput in) throws IOException {
212 final int length = in.readInt();
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);
220 payload = new Chunked(ChunkedByteArray.readFrom(in, length, MAX_ARRAY_SIZE));
224 private Object readResolve() {
225 return verifyNotNull(payload);