Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / AE.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 com.google.common.collect.ImmutableList;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import org.opendaylight.controller.cluster.raft.RaftVersions;
19 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
20 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * Argon serialization proxy for {@link AppendEntries}.
25  */
26 final class AE implements Externalizable {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
30     private AppendEntries appendEntries;
31
32     @SuppressWarnings("checkstyle:RedundantModifier")
33     public AE() {
34         // For Externalizable
35     }
36
37     AE(final AppendEntries appendEntries) {
38         this.appendEntries = requireNonNull(appendEntries);
39     }
40
41     @Override
42     public void writeExternal(final ObjectOutput out) throws IOException {
43         out.writeShort(appendEntries.getLeaderRaftVersion());
44         WritableObjects.writeLong(out, appendEntries.getTerm());
45         out.writeObject(appendEntries.getLeaderId());
46
47         WritableObjects.writeLongs(out, appendEntries.getPrevLogTerm(), appendEntries.getPrevLogIndex());
48         WritableObjects.writeLongs(out, appendEntries.getLeaderCommit(), appendEntries.getReplicatedToAllIndex());
49
50         out.writeShort(appendEntries.getPayloadVersion());
51
52         final var entries = appendEntries.getEntries();
53         out.writeInt(entries.size());
54         for (var e : entries) {
55             WritableObjects.writeLongs(out, e.getIndex(), e.getTerm());
56             out.writeObject(e.getData());
57         }
58
59         out.writeObject(appendEntries.leaderAddress());
60     }
61
62     @Override
63     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
64         short leaderRaftVersion = in.readShort();
65         long term = WritableObjects.readLong(in);
66         String leaderId = (String) in.readObject();
67
68         byte hdr = WritableObjects.readLongHeader(in);
69         long prevLogTerm = WritableObjects.readFirstLong(in, hdr);
70         long prevLogIndex = WritableObjects.readSecondLong(in, hdr);
71
72         hdr = WritableObjects.readLongHeader(in);
73         long leaderCommit = WritableObjects.readFirstLong(in, hdr);
74         long replicatedToAllIndex = WritableObjects.readSecondLong(in, hdr);
75         short payloadVersion = in.readShort();
76
77         int size = in.readInt();
78         var entries = ImmutableList.<ReplicatedLogEntry>builderWithExpectedSize(size);
79         for (int i = 0; i < size; i++) {
80             hdr = WritableObjects.readLongHeader(in);
81             entries.add(new SimpleReplicatedLogEntry(WritableObjects.readFirstLong(in, hdr),
82                 WritableObjects.readSecondLong(in, hdr), (Payload) in.readObject()));
83         }
84
85         String leaderAddress = (String)in.readObject();
86
87         appendEntries = new AppendEntries(term, leaderId, prevLogIndex, prevLogTerm, entries.build(), leaderCommit,
88                 replicatedToAllIndex, payloadVersion, RaftVersions.CURRENT_VERSION, leaderRaftVersion,
89                 leaderAddress);
90     }
91
92     @java.io.Serial
93     private Object readResolve() {
94         return verifyNotNull(appendEntries);
95     }
96 }