Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / SS.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.persisted;
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.ReplicatedLogEntry;
19 import org.opendaylight.controller.cluster.raft.messages.Payload;
20 import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
21 import org.opendaylight.yangtools.concepts.WritableObjects;
22
23 /**
24  * Externalizable proxy for {@link Snapshot}.
25  */
26 final class SS implements Externalizable {
27     @java.io.Serial
28     private static final long serialVersionUID = 1L;
29
30     private Snapshot snapshot;
31
32     @SuppressWarnings("checkstyle:RedundantModifier")
33     public SS() {
34         // For Externalizable
35     }
36
37     SS(final Snapshot snapshot) {
38         this.snapshot = requireNonNull(snapshot);
39     }
40
41     @Override
42     public void writeExternal(final ObjectOutput out) throws IOException {
43         WritableObjects.writeLongs(out, snapshot.getLastIndex(), snapshot.getLastTerm());
44         WritableObjects.writeLongs(out, snapshot.getLastAppliedIndex(), snapshot.getLastAppliedTerm());
45         WritableObjects.writeLong(out, snapshot.getElectionTerm());
46         out.writeObject(snapshot.getElectionVotedFor());
47         out.writeObject(snapshot.getServerConfiguration());
48
49         final var unAppliedEntries = snapshot.getUnAppliedEntries();
50         out.writeInt(unAppliedEntries.size());
51         for (var e : unAppliedEntries) {
52             WritableObjects.writeLongs(out, e.getIndex(), e.getTerm());
53             out.writeObject(e.getData());
54         }
55
56         out.writeObject(snapshot.getState());
57     }
58
59     @Override
60     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
61         byte hdr = WritableObjects.readLongHeader(in);
62         long lastIndex = WritableObjects.readFirstLong(in, hdr);
63         long lastTerm = WritableObjects.readSecondLong(in, hdr);
64
65         hdr = WritableObjects.readLongHeader(in);
66         long lastAppliedIndex = WritableObjects.readFirstLong(in, hdr);
67         long lastAppliedTerm = WritableObjects.readSecondLong(in, hdr);
68         long electionTerm = WritableObjects.readLong(in);
69         String electionVotedFor = (String) in.readObject();
70         ServerConfigurationPayload serverConfig = (ServerConfigurationPayload) in.readObject();
71
72         int size = in.readInt();
73         var unAppliedEntries = ImmutableList.<ReplicatedLogEntry>builderWithExpectedSize(size);
74         for (int i = 0; i < size; i++) {
75             hdr = WritableObjects.readLongHeader(in);
76             unAppliedEntries.add(new SimpleReplicatedLogEntry(
77                 WritableObjects.readFirstLong(in, hdr), WritableObjects.readSecondLong(in, hdr),
78                 (Payload) in.readObject()));
79         }
80
81         State state = (State) in.readObject();
82
83         snapshot = Snapshot.create(state, unAppliedEntries.build(), lastIndex, lastTerm, lastAppliedIndex,
84             lastAppliedTerm, electionTerm, electionVotedFor, serverConfig);
85     }
86
87     @java.io.Serial
88     private Object readResolve() {
89         return verifyNotNull(snapshot);
90     }
91 }