--- /dev/null
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.example.messages;
+
+import java.io.Serializable;
+
+final class KVv1 implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private final String key;
+ private final String value;
+
+ KVv1(String key, String value) {
+ this.key = key;
+ this.value = value;
+ }
+
+ Object readResolve() {
+ return new KeyValue(key, value);
+ }
+}
*/
package org.opendaylight.controller.cluster.example.messages;
-import java.io.Serializable;
import org.opendaylight.controller.cluster.raft.messages.Payload;
-public class KeyValue extends Payload implements Serializable {
+public final class KeyValue extends Payload {
private static final long serialVersionUID = 1L;
+
private String key;
private String value;
return value;
}
- public void setKey(String key) {
- this.key = key;
- }
-
- public void setValue(String value) {
- this.value = value;
- }
-
@Override
public String toString() {
return "KeyValue{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}';
public int size() {
return value.length() + key.length();
}
+
+ @Override
+ protected Object writeReplace() {
+ return new KVv1(value, key);
+ }
}
*/
package org.opendaylight.controller.cluster.raft.messages;
+import java.io.Serializable;
+
/**
* An instance of a {@link Payload} class is meant to be used as the Payload for {@link AppendEntries}.
*
* When an actor which is derived from RaftActor attempts to persistData it must pass an instance of the Payload class.
* Similarly when state needs to be applied to the derived RaftActor it will be passed an instance of the Payload class.
*/
-public abstract class Payload {
+public abstract class Payload implements Serializable {
+ private static final long serialVersionUID = 1L;
+
/**
* Return the estimate of in-memory size of this payload.
*
* @return An estimate of the in-memory size of this payload.
*/
public abstract int size();
+
+ /**
+ * Return the serialization proxy for this object.
+ *
+ * @return Serialization proxy
+ */
+ protected abstract Object writeReplace();
}
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
-import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
*
* @author Thomas Pantelis
*/
-public final class ServerConfigurationPayload extends Payload implements PersistentPayload, Serializable {
+public final class ServerConfigurationPayload extends Payload implements PersistentPayload {
private static final class Proxy implements Externalizable {
private static final long serialVersionUID = 1L;
}
Proxy(final ServerConfigurationPayload payload) {
- this.serverConfig = payload.getServerConfig();
+ serverConfig = payload.getServerConfig();
}
@Override
return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
}
- private Object writeReplace() {
+ @Override
+ protected Object writeReplace() {
return new Proxy(this);
}
}
package org.opendaylight.controller.cluster.raft;
+import static java.util.Objects.requireNonNull;
+
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import org.opendaylight.controller.cluster.DataPersistenceProvider;
@Override
public void update(final long newTerm, final String newVotedFor) {
- this.currentTerm = newTerm;
- this.votedFor = newVotedFor;
+ currentTerm = newTerm;
+ votedFor = newVotedFor;
// TODO : Write to some persistent state
}
}
@Override public ActorSystem getActorSystem() {
- return this.system;
+ return system;
}
@Override public ActorSelection getPeerActorSelection(final String peerId) {
}
}
- public static class MockPayload extends Payload implements Serializable {
+ public static final class MockPayload extends Payload {
private static final long serialVersionUID = 3121380393130864247L;
- private String value = "";
- private int size;
+
+ private final String data;
+ private final int size;
public MockPayload() {
+ this("");
}
public MockPayload(final String data) {
- this.value = data;
- size = value.length();
+ this(data, data.length());
}
public MockPayload(final String data, final int size) {
- this(data);
+ this.data = requireNonNull(data);
this.size = size;
}
@Override
public String toString() {
- return value;
+ return data;
}
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (value == null ? 0 : value.hashCode());
- return result;
+ return data.hashCode();
}
@Override
public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- MockPayload other = (MockPayload) obj;
- if (value == null) {
- if (other.value != null) {
- return false;
- }
- } else if (!value.equals(other.value)) {
- return false;
- }
- return true;
+ return this == obj || obj instanceof MockPayload other && Objects.equals(data, other.data)
+ && size == other.size;
+ }
+
+ @Override
+ protected Object writeReplace() {
+ return new MockPayloadProxy(data, size);
+ }
+ }
+
+ private static final class MockPayloadProxy implements Serializable {
+ private static final long serialVersionUID = 1L;
+
+ private final String value;
+ private final int size;
+
+ MockPayloadProxy(String value, int size) {
+ this.value = value;
+ this.size = size;
+ }
+
+ Object readResolve() {
+ return new MockPayload(value, size);
}
}
public MockReplicatedLogBuilder createEntries(final int start, final int end, final int term) {
for (int i = start; i < end; i++) {
- this.mockLog.append(new SimpleReplicatedLogEntry(i, term,
+ mockLog.append(new SimpleReplicatedLogEntry(i, term,
new MockRaftActorContext.MockPayload(Integer.toString(i))));
}
return this;
}
public MockReplicatedLogBuilder addEntry(final int index, final int term, final MockPayload payload) {
- this.mockLog.append(new SimpleReplicatedLogEntry(index, term, payload));
+ mockLog.append(new SimpleReplicatedLogEntry(index, term, payload));
return this;
}
public ReplicatedLog build() {
- return this.mockLog;
+ return mockLog;
}
}