2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft.persisted;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
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;
24 * Externalizable proxy for {@link Snapshot}.
26 final class SS implements Externalizable {
28 private static final long serialVersionUID = 1L;
30 private Snapshot snapshot;
32 @SuppressWarnings("checkstyle:RedundantModifier")
37 SS(final Snapshot snapshot) {
38 this.snapshot = requireNonNull(snapshot);
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());
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());
56 out.writeObject(snapshot.getState());
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);
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();
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()));
81 State state = (State) in.readObject();
83 snapshot = Snapshot.create(state, unAppliedEntries.build(), lastIndex, lastTerm, lastAppliedIndex,
84 lastAppliedTerm, electionTerm, electionVotedFor, serverConfig);
88 private Object readResolve() {
89 return verifyNotNull(snapshot);