Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / SkipTransactionsRequest.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.access.commands;
9
10 import akka.actor.ActorRef;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.primitives.UnsignedLong;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.util.Collection;
18 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.controller.cluster.access.ABIVersion;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.yangtools.concepts.WritableObjects;
23
24 /**
25  * Request to skip a number of {@link TransactionIdentifier}s within a {code local history}. This request is essentially
26  * equivalent to {@link TransactionPurgeRequest} for {@link #getTarget()}, but also carries additional sibling
27  * {@link TransactionIdentifier}s in {@link #getOthers()}.
28  *
29  * <p>
30  * This request is sent by the frontend to inform the backend that a set of {@link TransactionIdentifier}s are
31  * explicitly retired and are guaranteed to never be used by the frontend.
32  */
33 public final class SkipTransactionsRequest extends TransactionRequest<SkipTransactionsRequest> {
34     interface SerialForm extends TransactionRequest.SerialForm<SkipTransactionsRequest> {
35         @Override
36         default SkipTransactionsRequest readExternal(final ObjectInput in, final TransactionIdentifier target,
37                 final long sequence, final ActorRef replyTo) throws IOException {
38             final int size = in.readInt();
39             final var builder = ImmutableList.<UnsignedLong>builderWithExpectedSize(size);
40             int idx;
41             if (size % 2 != 0) {
42                 builder.add(UnsignedLong.fromLongBits(WritableObjects.readLong(in)));
43                 idx = 1;
44             } else {
45                 idx = 0;
46             }
47             for (; idx < size; idx += 2) {
48                 final byte hdr = WritableObjects.readLongHeader(in);
49                 builder.add(UnsignedLong.fromLongBits(WritableObjects.readFirstLong(in, hdr)));
50                 builder.add(UnsignedLong.fromLongBits(WritableObjects.readSecondLong(in, hdr)));
51             }
52
53             return new SkipTransactionsRequest(target, sequence, replyTo, builder.build());
54         }
55
56         @Override
57         default void writeExternal(final ObjectOutput out, final SkipTransactionsRequest msg) throws IOException {
58             TransactionRequest.SerialForm.super.writeExternal(out, msg);
59
60             final var others = msg.others;
61             final int size = others.size();
62             out.writeInt(size);
63
64             int idx;
65             if (size % 2 != 0) {
66                 WritableObjects.writeLong(out, others.get(0).longValue());
67                 idx = 1;
68             } else {
69                 idx = 0;
70             }
71             for (; idx < size; idx += 2) {
72                 WritableObjects.writeLongs(out, others.get(idx).longValue(), others.get(idx + 1).longValue());
73             }
74         }
75     }
76
77     @java.io.Serial
78     private static final long serialVersionUID = 1L;
79
80     // Note: UnsignedLong is arbitrary, yang.common.Uint64 would work just as well, we really want an immutable
81     //       List<long>, though.
82     private final @NonNull ImmutableList<UnsignedLong> others;
83
84     public SkipTransactionsRequest(final TransactionIdentifier target, final long sequence,
85             final ActorRef replyTo, final Collection<UnsignedLong> others) {
86         super(target, sequence, replyTo);
87         this.others = ImmutableList.copyOf(others);
88     }
89
90     private SkipTransactionsRequest(final SkipTransactionsRequest request, final ABIVersion version) {
91         super(request, version);
92         others = request.others;
93     }
94
95     /**
96      * Return this {@link #getTarget()}s sibling {@link TransactionIdentifier}s.
97      *
98      * @return Siblings values of {@link TransactionIdentifier#getTransactionId()}
99      */
100     public List<UnsignedLong> getOthers() {
101         return others;
102     }
103
104     @Override
105     protected SerialForm externalizableProxy(final ABIVersion version) {
106         return new STR(this);
107     }
108
109     @Override
110     protected SkipTransactionsRequest cloneAsVersion(final ABIVersion version) {
111         return new SkipTransactionsRequest(this, version);
112     }
113
114     @Override
115     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
116         final var helper = super.addToStringAttributes(toStringHelper);
117         if (!others.isEmpty()) {
118             helper.add("others", others);
119         }
120         return helper;
121     }
122 }