Track skipped transactions
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / SkipTransactionsPayload.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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
13 import com.google.common.io.ByteStreams;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.DataInput;
16 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
19 import org.opendaylight.controller.cluster.datastore.utils.ImmutableUnsignedLongSet;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Payload persisted when a local history is instructed some transaction identifiers, i.e. the frontend has used them
25  * for other purposes. It contains a {@link LocalHistoryIdentifier} and a list of transaction identifiers within that
26  * local history.
27  */
28 public final class SkipTransactionsPayload extends AbstractIdentifiablePayload<LocalHistoryIdentifier> {
29     private static final class Proxy extends AbstractProxy<LocalHistoryIdentifier> {
30         private static final long serialVersionUID = 1L;
31
32         private ImmutableUnsignedLongSet transactionIds;
33
34         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
35         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
36         @SuppressWarnings("checkstyle:RedundantModifier")
37         public Proxy() {
38             // For Externalizable
39         }
40
41         Proxy(final byte[] serialized) {
42             super(serialized);
43         }
44
45         @Override
46         protected LocalHistoryIdentifier readIdentifier(final DataInput in) throws IOException {
47             final var id = LocalHistoryIdentifier.readFrom(in);
48             transactionIds = ImmutableUnsignedLongSet.readFrom(in);
49             return id;
50         }
51
52         @Override
53         protected SkipTransactionsPayload createObject(final LocalHistoryIdentifier identifier,
54                 final byte[] serialized) {
55             return new SkipTransactionsPayload(identifier, serialized, verifyNotNull(transactionIds));
56         }
57     }
58
59     private static final Logger LOG = LoggerFactory.getLogger(SkipTransactionsPayload.class);
60     private static final long serialVersionUID = 1L;
61
62     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled via externalizable proxy")
63     private final @NonNull ImmutableUnsignedLongSet transactionIds;
64
65     private SkipTransactionsPayload(final @NonNull LocalHistoryIdentifier historyId,
66             final byte @NonNull [] serialized, final ImmutableUnsignedLongSet transactionIds) {
67         super(historyId, serialized);
68         this.transactionIds = requireNonNull(transactionIds);
69     }
70
71     public static @NonNull SkipTransactionsPayload create(final LocalHistoryIdentifier historyId,
72             final ImmutableUnsignedLongSet transactionIds, final int initialSerializedBufferCapacity) {
73         final var out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
74         try {
75             historyId.writeTo(out);
76             transactionIds.writeTo(out);
77         } catch (IOException e) {
78             // This should never happen
79             LOG.error("Failed to serialize {} ids {}", historyId, transactionIds, e);
80             throw new RuntimeException("Failed to serialize " + historyId + " ids " + transactionIds, e);
81         }
82
83         return new SkipTransactionsPayload(historyId, out.toByteArray(), transactionIds);
84     }
85
86     public @NonNull ImmutableUnsignedLongSet getTransactionIds() {
87         return transactionIds;
88     }
89
90     @Override
91     protected Proxy externalizableProxy(final byte[] serialized) {
92         return new Proxy(serialized);
93     }
94 }