+public abstract class CommitTransactionPayload extends IdentifiablePayload<TransactionIdentifier>
+ implements Serializable {
+ private static final Logger LOG = LoggerFactory.getLogger(CommitTransactionPayload.class);
+ private static final long serialVersionUID = 1L;
+
+ private static final int MAX_ARRAY_SIZE = ceilingPowerOfTwo(Integer.getInteger(
+ "org.opendaylight.controller.cluster.datastore.persisted.max-array-size", 256 * 1024));
+
+ private volatile Entry<TransactionIdentifier, DataTreeCandidateWithVersion> candidate = null;
+
+ CommitTransactionPayload() {
+
+ }
+
+ public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
+ final DataTreeCandidate candidate, final PayloadVersion version, final int initialSerializedBufferCapacity)
+ throws IOException {
+ final ChunkedOutputStream cos = new ChunkedOutputStream(initialSerializedBufferCapacity, MAX_ARRAY_SIZE);
+ try (DataOutputStream dos = new DataOutputStream(cos)) {
+ transactionId.writeTo(dos);
+ DataTreeCandidateInputOutput.writeDataTreeCandidate(dos, version, candidate);
+ }
+
+ final Either<byte[], ChunkedByteArray> source = cos.toVariant();
+ LOG.debug("Initial buffer capacity {}, actual serialized size {}", initialSerializedBufferCapacity, cos.size());
+ return source.isFirst() ? new Simple(source.getFirst()) : new Chunked(source.getSecond());
+ }
+
+ @VisibleForTesting
+ public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
+ final DataTreeCandidate candidate, final PayloadVersion version) throws IOException {
+ return create(transactionId, candidate, version, 512);
+ }
+
+ @VisibleForTesting
+ public static @NonNull CommitTransactionPayload create(final TransactionIdentifier transactionId,
+ final DataTreeCandidate candidate) throws IOException {
+ return create(transactionId, candidate, PayloadVersion.current());
+ }
+
+ public @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate() throws IOException {
+ Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = candidate;
+ if (localCandidate == null) {
+ synchronized (this) {
+ localCandidate = candidate;
+ if (localCandidate == null) {
+ candidate = localCandidate = getCandidate(ReusableImmutableNormalizedNodeStreamWriter.create());
+ }
+ }
+ }
+ return localCandidate;
+ }
+
+ public final @NonNull Entry<TransactionIdentifier, DataTreeCandidateWithVersion> getCandidate(
+ final ReusableStreamReceiver receiver) throws IOException {
+ final DataInput in = newDataInput();
+ return new SimpleImmutableEntry<>(TransactionIdentifier.readFrom(in),
+ DataTreeCandidateInputOutput.readDataTreeCandidate(in, receiver));
+ }
+
+ @Override
+ public TransactionIdentifier getIdentifier() {
+ try {
+ return getCandidate().getKey();
+ } catch (IOException e) {
+ throw new IllegalStateException("Candidate deserialization failed.", e);
+ }
+ }
+
+ /**
+ * The cached candidate needs to be cleared after it is done applying to the DataTree, otherwise it would be keeping
+ * deserialized in memory which are not needed anymore leading to wasted memory. This lets the payload know that
+ * this was the last time the candidate was needed ant it is safe to be cleared.
+ */
+ public Entry<TransactionIdentifier, DataTreeCandidateWithVersion> acquireCandidate() throws IOException {
+ final Entry<TransactionIdentifier, DataTreeCandidateWithVersion> localCandidate = getCandidate();
+ candidate = null;
+ return localCandidate;
+ }
+
+ abstract void writeBytes(ObjectOutput out) throws IOException;
+
+ abstract DataInput newDataInput();
+
+ final Object writeReplace() {
+ return new Proxy(this);
+ }
+
+ private static final class Simple extends CommitTransactionPayload {
+ private static final long serialVersionUID = 1L;
+
+ private final byte[] serialized;
+
+ Simple(final byte[] serialized) {
+ this.serialized = requireNonNull(serialized);
+ }
+
+ @Override
+ public int size() {
+ return serialized.length;
+ }
+
+ @Override
+ DataInput newDataInput() {
+ return ByteStreams.newDataInput(serialized);
+ }
+
+ @Override
+ void writeBytes(final ObjectOutput out) throws IOException {
+ out.write(serialized);
+ }
+ }
+
+ private static final class Chunked extends CommitTransactionPayload {
+ private static final long serialVersionUID = 1L;
+
+ @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via serialization proxy")
+ private final ChunkedByteArray source;
+
+ Chunked(final ChunkedByteArray source) {
+ this.source = requireNonNull(source);
+ }
+
+ @Override
+ void writeBytes(final ObjectOutput out) throws IOException {
+ source.copyTo(out);
+ }
+
+ @Override
+ public int size() {
+ return source.size();
+ }
+
+ @Override
+ DataInput newDataInput() {
+ return new DataInputStream(source.openStream());
+ }
+ }
+