Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / IS.java
1 /*
2  * Copyright (c) 2022 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.raft.messages;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.util.Optional;
18 import java.util.OptionalInt;
19 import org.opendaylight.controller.cluster.raft.RaftVersions;
20 import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * Serialization proxy for {@link InstallSnapshot}.
25  */
26 final class IS implements Externalizable {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
30     // Flags
31     private static final int LAST_CHUNK_HASHCODE = 0x10;
32     private static final int SERVER_CONFIG       = 0x20;
33
34     private InstallSnapshot installSnapshot;
35
36     @SuppressWarnings("checkstyle:RedundantModifier")
37     public IS() {
38         // For Externalizable
39     }
40
41     IS(final InstallSnapshot installSnapshot) {
42         this.installSnapshot = requireNonNull(installSnapshot);
43     }
44
45     @Override
46     public void writeExternal(final ObjectOutput out) throws IOException {
47         int flags = 0;
48         final var lastChunkHashCode = installSnapshot.getLastChunkHashCode();
49         if (lastChunkHashCode.isPresent()) {
50             flags |= LAST_CHUNK_HASHCODE;
51         }
52         final var serverConfig = installSnapshot.getServerConfig();
53         if (serverConfig.isPresent()) {
54             flags |= SERVER_CONFIG;
55         }
56
57         WritableObjects.writeLong(out, installSnapshot.getTerm(), flags);
58         out.writeObject(installSnapshot.getLeaderId());
59         WritableObjects.writeLongs(out, installSnapshot.getLastIncludedIndex(), installSnapshot.getLastIncludedTerm());
60         out.writeInt(installSnapshot.getChunkIndex());
61         out.writeInt(installSnapshot.getTotalChunks());
62
63         if (lastChunkHashCode.isPresent()) {
64             out.writeInt(lastChunkHashCode.orElseThrow());
65         }
66         if (serverConfig.isPresent()) {
67             out.writeObject(serverConfig.orElseThrow());
68         }
69
70         out.writeObject(installSnapshot.getData());
71     }
72
73     @Override
74     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
75         byte hdr = WritableObjects.readLongHeader(in);
76         final int flags = WritableObjects.longHeaderFlags(hdr);
77
78         long term = WritableObjects.readLongBody(in, hdr);
79         String leaderId = (String) in.readObject();
80
81         hdr = WritableObjects.readLongHeader(in);
82         long lastIncludedIndex = WritableObjects.readFirstLong(in, hdr);
83         long lastIncludedTerm = WritableObjects.readSecondLong(in, hdr);
84         int chunkIndex = in.readInt();
85         int totalChunks = in.readInt();
86
87         OptionalInt lastChunkHashCode = getFlag(flags, LAST_CHUNK_HASHCODE) ? OptionalInt.of(in.readInt())
88             : OptionalInt.empty();
89         Optional<ServerConfigurationPayload> serverConfig = getFlag(flags, SERVER_CONFIG)
90                 ? Optional.of((ServerConfigurationPayload)in.readObject()) : Optional.empty();
91
92         byte[] data = (byte[])in.readObject();
93
94         installSnapshot = new InstallSnapshot(term, leaderId, lastIncludedIndex, lastIncludedTerm, data,
95                 chunkIndex, totalChunks, lastChunkHashCode, serverConfig, RaftVersions.CURRENT_VERSION);
96     }
97
98     @java.io.Serial
99     private Object readResolve() {
100         return verifyNotNull(installSnapshot);
101     }
102
103     private static boolean getFlag(final int flags, final int bit) {
104         return (flags & bit) != 0;
105     }
106 }
107