import akka.actor.ActorRef;
import akka.actor.Props;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
import com.google.common.io.ByteSource;
import com.google.common.util.concurrent.Uninterruptibles;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
-import javax.annotation.Nonnull;
+import java.util.function.Function;
import org.apache.commons.lang3.SerializationUtils;
import org.opendaylight.controller.cluster.DataPersistenceProvider;
import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
+import org.opendaylight.controller.cluster.raft.messages.Payload;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
-import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
import org.opendaylight.yangtools.concepts.Identifier;
public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
final CountDownLatch snapshotCommitted = new CountDownLatch(1);
private final Function<Runnable, Void> pauseLeaderFunction;
- protected MockRaftActor(AbstractBuilder<?, ?> builder) {
+ protected MockRaftActor(final AbstractBuilder<?, ?> builder) {
super(builder.id, builder.peerAddresses != null ? builder.peerAddresses :
- Collections.<String, String>emptyMap(), Optional.fromNullable(builder.config), PAYLOAD_VERSION);
- state = new ArrayList<>();
- this.actorDelegate = mock(RaftActor.class);
- this.recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
+ Collections.emptyMap(), Optional.ofNullable(builder.config), PAYLOAD_VERSION);
+ state = Collections.synchronizedList(new ArrayList<>());
+ actorDelegate = mock(RaftActor.class);
+ recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
- this.snapshotCohortDelegate = builder.snapshotCohort != null ? builder.snapshotCohort :
+ snapshotCohortDelegate = builder.snapshotCohort != null ? builder.snapshotCohort :
mock(RaftActorSnapshotCohort.class);
if (builder.dataPersistenceProvider == null) {
- setPersistence(builder.persistent.isPresent() ? builder.persistent.get() : true);
+ setPersistence(builder.persistent.isPresent() ? builder.persistent.orElseThrow() : true);
} else {
setPersistence(builder.dataPersistenceProvider);
}
pauseLeaderFunction = builder.pauseLeaderFunction;
}
- public void setRaftActorRecoverySupport(RaftActorRecoverySupport support) {
+ public void setRaftActorRecoverySupport(final RaftActorRecoverySupport support) {
raftActorRecoverySupport = support;
}
try {
assertEquals("Recovery complete", true, recoveryComplete.await(5, TimeUnit.SECONDS));
} catch (InterruptedException e) {
- Throwables.propagate(e);
+ throw new RuntimeException(e);
}
}
try {
assertEquals("Behavior initialized", true, initializeBehaviorComplete.await(5, TimeUnit.SECONDS));
} catch (InterruptedException e) {
- Throwables.propagate(e);
+ throw new RuntimeException(e);
}
}
}
@Override
- protected void applyState(ActorRef clientActor, Identifier identifier, Object data) {
+ protected void applyState(final ActorRef clientActor, final Identifier identifier, final Object data) {
actorDelegate.applyState(clientActor, identifier, data);
LOG.info("{}: applyState called: {}", persistenceId(), data);
}
@Override
- @Nonnull
protected RaftActorRecoveryCohort getRaftActorRecoveryCohort() {
return this;
}
}
@Override
- public void startLogRecoveryBatch(int maxBatchSize) {
+ public void startLogRecoveryBatch(final int maxBatchSize) {
}
@Override
- public void appendRecoveredLogEntry(Payload data) {
+ public void appendRecoveredLogEntry(final Payload data) {
state.add(data);
}
}
@Override
- public void applyRecoverySnapshot(Snapshot.State newState) {
+ public void applyRecoverySnapshot(final Snapshot.State newState) {
recoveryCohortDelegate.applyRecoverySnapshot(newState);
applySnapshotState(newState);
}
- private void applySnapshotState(Snapshot.State newState) {
- if (newState instanceof MockSnapshotState) {
+ private void applySnapshotState(final Snapshot.State newState) {
+ if (newState instanceof MockSnapshotState mockState) {
state.clear();
- state.addAll(((MockSnapshotState)newState).getState());
+ state.addAll(mockState.getState());
}
}
@Override
- public void createSnapshot(ActorRef actorRef, java.util.Optional<OutputStream> installSnapshotStream) {
+ public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
LOG.info("{}: createSnapshot called", persistenceId());
snapshotCohortDelegate.createSnapshot(actorRef, installSnapshotStream);
}
@Override
- public void applySnapshot(Snapshot.State newState) {
+ public void applySnapshot(final Snapshot.State newState) {
LOG.info("{}: applySnapshot called", persistenceId());
applySnapshotState(newState);
snapshotCohortDelegate.applySnapshot(newState);
}
@Override
- public Snapshot.State deserializeSnapshot(ByteSource snapshotBytes) {
+ public Snapshot.State deserializeSnapshot(final ByteSource snapshotBytes) {
try {
return (Snapshot.State) SerializationUtils.deserialize(snapshotBytes.read());
} catch (IOException e) {
}
}
- @Override
- public Snapshot.State deserializePreCarbonSnapshot(byte[] from) {
- return new MockSnapshotState(SerializationUtils.deserialize(from));
- }
-
@Override
protected void onStateChanged() {
actorDelegate.onStateChanged();
@Override
protected Optional<ActorRef> getRoleChangeNotifier() {
- return Optional.fromNullable(roleChangeNotifier);
+ return Optional.ofNullable(roleChangeNotifier);
}
@Override public String persistenceId() {
- return this.getId();
+ return getId();
}
- protected void newBehavior(RaftActorBehavior newBehavior) {
+ protected void newBehavior(final RaftActorBehavior newBehavior) {
self().tell(newBehavior, ActorRef.noSender());
}
}
@Override
- protected void pauseLeader(Runnable operation) {
+ protected void pauseLeader(final Runnable operation) {
if (pauseLeaderFunction != null) {
pauseLeaderFunction.apply(operation);
} else {
}
}
- public static List<Object> fromState(Snapshot.State from) {
- if (from instanceof MockSnapshotState) {
- return ((MockSnapshotState)from).getState();
+ public static List<Object> fromState(final Snapshot.State from) {
+ if (from instanceof MockSnapshotState mockState) {
+ return mockState.getState();
}
throw new IllegalStateException("Unexpected snapshot State: " + from);
}
public ReplicatedLog getReplicatedLog() {
- return this.getRaftActorContext().getReplicatedLog();
+ return getRaftActorContext().getReplicatedLog();
}
@Override
return restoreFromSnapshot;
}
- public static Props props(final String id, final Map<String, String> peerAddresses, ConfigParams config) {
+ public static Props props(final String id, final Map<String, String> peerAddresses, final ConfigParams config) {
return builder().id(id).peerAddresses(peerAddresses).config(config).props();
}
public static Props props(final String id, final Map<String, String> peerAddresses,
- ConfigParams config, DataPersistenceProvider dataPersistenceProvider) {
+ final ConfigParams config, final DataPersistenceProvider dataPersistenceProvider) {
return builder().id(id).peerAddresses(peerAddresses).config(config)
.dataPersistenceProvider(dataPersistenceProvider).props();
}
private ActorRef roleChangeNotifier;
private RaftActorSnapshotMessageSupport snapshotMessageSupport;
private Snapshot restoreFromSnapshot;
- private Optional<Boolean> persistent = Optional.absent();
+ private Optional<Boolean> persistent = Optional.empty();
private final Class<A> actorClass;
private Function<Runnable, Void> pauseLeaderFunction;
private RaftActorSnapshotCohort snapshotCohort;
- protected AbstractBuilder(Class<A> actorClass) {
+ protected AbstractBuilder(final Class<A> actorClass) {
this.actorClass = actorClass;
}
return (T) this;
}
- public T id(String newId) {
- this.id = newId;
+ public T id(final String newId) {
+ id = newId;
return self();
}
- public T peerAddresses(Map<String, String> newPeerAddresses) {
- this.peerAddresses = newPeerAddresses;
+ public T peerAddresses(final Map<String, String> newPeerAddresses) {
+ peerAddresses = newPeerAddresses;
return self();
}
- public T config(ConfigParams newConfig) {
- this.config = newConfig;
+ public T config(final ConfigParams newConfig) {
+ config = newConfig;
return self();
}
- public T dataPersistenceProvider(DataPersistenceProvider newDataPersistenceProvider) {
- this.dataPersistenceProvider = newDataPersistenceProvider;
+ public T dataPersistenceProvider(final DataPersistenceProvider newDataPersistenceProvider) {
+ dataPersistenceProvider = newDataPersistenceProvider;
return self();
}
- public T roleChangeNotifier(ActorRef newRoleChangeNotifier) {
- this.roleChangeNotifier = newRoleChangeNotifier;
+ public T roleChangeNotifier(final ActorRef newRoleChangeNotifier) {
+ roleChangeNotifier = newRoleChangeNotifier;
return self();
}
- public T snapshotMessageSupport(RaftActorSnapshotMessageSupport newSnapshotMessageSupport) {
- this.snapshotMessageSupport = newSnapshotMessageSupport;
+ public T snapshotMessageSupport(final RaftActorSnapshotMessageSupport newSnapshotMessageSupport) {
+ snapshotMessageSupport = newSnapshotMessageSupport;
return self();
}
- public T restoreFromSnapshot(Snapshot newRestoreFromSnapshot) {
- this.restoreFromSnapshot = newRestoreFromSnapshot;
+ public T restoreFromSnapshot(final Snapshot newRestoreFromSnapshot) {
+ restoreFromSnapshot = newRestoreFromSnapshot;
return self();
}
- public T persistent(Optional<Boolean> newPersistent) {
- this.persistent = newPersistent;
+ public T persistent(final Optional<Boolean> newPersistent) {
+ persistent = newPersistent;
return self();
}
- public T pauseLeaderFunction(Function<Runnable, Void> newPauseLeaderFunction) {
- this.pauseLeaderFunction = newPauseLeaderFunction;
+ public T pauseLeaderFunction(final Function<Runnable, Void> newPauseLeaderFunction) {
+ pauseLeaderFunction = newPauseLeaderFunction;
return self();
}
- public T snapshotCohort(RaftActorSnapshotCohort newSnapshotCohort) {
- this.snapshotCohort = newSnapshotCohort;
+ public T snapshotCohort(final RaftActorSnapshotCohort newSnapshotCohort) {
+ snapshotCohort = newSnapshotCohort;
return self();
}
}
public static class Builder extends AbstractBuilder<Builder, MockRaftActor> {
- private Builder() {
+ Builder() {
super(MockRaftActor.class);
}
}
private final List<Object> state;
- public MockSnapshotState(List<Object> state) {
+ public MockSnapshotState(final List<Object> state) {
this.state = state;
}
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (state == null ? 0 : state.hashCode());
- return result;
+ return Objects.hash(state);
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
return false;
}
MockSnapshotState other = (MockSnapshotState) obj;
- if (state == null) {
- if (other.state != null) {
- return false;
- }
- } else if (!state.equals(other.state)) {
+ if (!Objects.equals(state, other.state)) {
return false;
}
return true;