Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbstractIdentifiablePayload.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.base.MoreObjects;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.function.Function;
19 import org.apache.commons.lang3.SerializationUtils;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.raft.messages.IdentifiablePayload;
22 import org.opendaylight.yangtools.concepts.Identifier;
23
24 /**
25  * Abstract base class for {@link IdentifiablePayload}s which hold a single {@link Identifier}.
26  */
27 public abstract class AbstractIdentifiablePayload<T extends Identifier> extends IdentifiablePayload<T> {
28     /**
29      * An {@link Externalizable} with default implementations we expect our implementations to comply with. On-wire
30      * serialization format is defined by {@link #bytes()}.
31      */
32     protected interface SerialForm extends Externalizable {
33         /**
34          * Return the serial form of this object contents, corresponding to
35          * {@link AbstractIdentifiablePayload#serialized}.
36          *
37          * @return Serialized form
38          */
39         byte[] bytes();
40
41         /**
42          * Resolve this proxy to an actual {@link AbstractIdentifiablePayload}.
43          *
44          * @return A payload.
45          */
46         @java.io.Serial
47         Object readResolve();
48
49         /**
50          * Restore state from specified serialized form.
51          *
52          * @param newBytes Serialized form, as returned by {@link #bytes()}
53          * @throws IOException when a deserialization problem occurs
54          */
55         void readExternal(byte[] newBytes) throws IOException;
56
57         /**
58          * {@inheritDoc}
59          *
60          * <p>
61          * The default implementation is canonical and should never be overridden.
62          */
63         @Override
64         default void readExternal(final ObjectInput in) throws IOException {
65             final var bytes = new byte[in.readInt()];
66             in.readFully(bytes);
67             readExternal(bytes);
68         }
69
70         /**
71          * {@inheritDoc}
72          *
73          * <p>
74          * The default implementation is canonical and should never be overridden.
75          */
76         @Override
77         default void writeExternal(final ObjectOutput out) throws IOException {
78             final var bytes = bytes();
79             out.writeInt(bytes.length);
80             out.write(bytes);
81         }
82     }
83
84     @java.io.Serial
85     private static final long serialVersionUID = 1L;
86
87     private final byte @NonNull [] serialized;
88     private final @NonNull T identifier;
89
90     AbstractIdentifiablePayload(final @NonNull T identifier, final byte @NonNull[] serialized) {
91         this.identifier = requireNonNull(identifier);
92         this.serialized = requireNonNull(serialized);
93     }
94
95     @Override
96     public final T getIdentifier() {
97         return identifier;
98     }
99
100     @Override
101     public final int size() {
102         return serialized.length;
103     }
104
105     protected final byte @NonNull [] serialized() {
106         return serialized;
107     }
108
109     @Override
110     public final int serializedSize() {
111         // TODO: this is not entirely accurate, as the serialization stream has additional overheads:
112         //       - 3 bytes for each block of data <256 bytes
113         //       - 5 bytes for each block of data >=256 bytes
114         //       - each block of data is limited to 1024 bytes as per serialization spec
115         return size() + externalizableProxySize();
116     }
117
118     @Override
119     public final String toString() {
120         return MoreObjects.toStringHelper(this).add("identifier", identifier).add("size", size()).toString();
121     }
122
123     @Override
124     public final Object writeReplace() {
125         return verifyNotNull(externalizableProxy(serialized));
126     }
127
128     protected abstract @NonNull SerialForm externalizableProxy(byte @NonNull[] serialized);
129
130     protected abstract int externalizableProxySize();
131
132     protected static final int externalizableProxySize(final Function<byte[], ? extends SerialForm> constructor) {
133         return SerializationUtils.serialize(constructor.apply(new byte[0])).length;
134     }
135 }