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.math.IntMath.ceilingPowerOfTwo;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.MoreObjects;
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.IOException;
22 import java.io.ObjectOutput;
23 import java.io.Serializable;
24 import java.util.AbstractMap.SimpleImmutableEntry;
25 import java.util.Map.Entry;
26 import org.apache.commons.lang3.SerializationUtils;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
29 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateInputOutput.DataTreeCandidateWithVersion;
30 import org.opendaylight.controller.cluster.io.ChunkedByteArray;
31 import org.opendaylight.controller.cluster.io.ChunkedOutputStream;
32 import org.opendaylight.controller.cluster.raft.messages.IdentifiablePayload;
33 import org.opendaylight.yangtools.concepts.Either;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Payload persisted when a transaction commits. It contains the transaction identifier and the
42 * {@link DataTreeCandidate}
44 * @author Robert Varga
47 public abstract sealed 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;
52 static final int MAX_ARRAY_SIZE = ceilingPowerOfTwo(Integer.getInteger(
53 "org.opendaylight.controller.cluster.datastore.persisted.max-array-size", 256 * 1024));
55 private volatile Entry<TransactionIdentifier, DataTreeCandidateWithVersion> candidate = null;
57 CommitTransactionPayload() {
61 public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
62 final DataTreeCandidate candidate, final PayloadVersion version, final int initialSerializedBufferCapacity)
64 final ChunkedOutputStream cos = new ChunkedOutputStream(initialSerializedBufferCapacity, MAX_ARRAY_SIZE);
65 try (DataOutputStream dos = new DataOutputStream(cos)) {
66 transactionId.writeTo(dos);
67 DataTreeCandidateInputOutput.writeDataTreeCandidate(dos, version, candidate);
70 final Either<byte[], ChunkedByteArray> source = cos.toVariant();
71 LOG.debug("Initial buffer capacity {}, actual serialized size {}", initialSerializedBufferCapacity, cos.size());
72 return source.isFirst() ? new Simple(source.getFirst()) : new Chunked(source.getSecond());
76 public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
77 final DataTreeCandidate candidate, final PayloadVersion version) throws IOException {
78 return create(transactionId, candidate, version, 512);
82 public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
83 final DataTreeCandidate candidate) throws IOException {
84 return create(transactionId, candidate, PayloadVersion.current());
87 public @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate() throws IOException {
88 Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = candidate;
89 if (localCandidate == null) {
91 localCandidate = candidate;
92 if (localCandidate == null) {
93 candidate = localCandidate = getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
97 return localCandidate;
100 public final @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate(
101 final ReusableStreamReceiver receiver) throws IOException {
102 final DataInput in = newDataInput();
103 return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
104 DataTreeCandidateInputOutput.readDataTreeCandidate(in, receiver));
108 public TransactionIdentifier getIdentifier() {
110 return getCandidate().getKey();
111 } catch (IOException e) {
112 throw new IllegalStateException("Candidate deserialization failed.", e);
117 public final int serializedSize() {
118 // TODO: this is not entirely accurate as the the byte[] can be chunked by the serialization stream
119 return ProxySizeHolder.PROXY_SIZE + size();
123 * The cached candidate needs to be cleared after it is done applying to the DataTree, otherwise it would be keeping
124 * deserialized in memory which are not needed anymore leading to wasted memory. This lets the payload know that
125 * this was the last time the candidate was needed ant it is safe to be cleared.
127 public Entry<TransactionIdentifier, DataTreeCandidateWithVersion> acquireCandidate() throws IOException {
128 final Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = getCandidate();
130 return localCandidate;
134 public final String toString() {
135 final var helper = MoreObjects.toStringHelper(this);
136 final var localCandidate = candidate;
137 if (localCandidate != null) {
138 helper.add("identifier", candidate.getKey());
140 return helper.add("size", size()).toString();
143 abstract void writeBytes(ObjectOutput out) throws IOException;
145 abstract DataInput newDataInput();
148 public final Object writeReplace() {
152 static final class Simple extends CommitTransactionPayload {
154 private static final long serialVersionUID = 1L;
156 private final byte[] serialized;
158 Simple(final byte[] serialized) {
159 this.serialized = requireNonNull(serialized);
164 return serialized.length;
168 DataInput newDataInput() {
169 return ByteStreams.newDataInput(serialized);
173 void writeBytes(final ObjectOutput out) throws IOException {
174 out.write(serialized);
178 static final class Chunked extends CommitTransactionPayload {
180 private static final long serialVersionUID = 1L;
182 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via serialization proxy")
183 private final ChunkedByteArray source;
185 Chunked(final ChunkedByteArray source) {
186 this.source = requireNonNull(source);
190 void writeBytes(final ObjectOutput out) throws IOException {
196 return source.size();
200 DataInput newDataInput() {
201 return new DataInputStream(source.openStream());
205 // Exists to break initialization dependency between CommitTransactionPayload/Simple/Proxy
206 private static final class ProxySizeHolder {
207 static final int PROXY_SIZE = SerializationUtils.serialize(new CT(new Simple(new byte[0]))).length;
209 private ProxySizeHolder() {